Skip to content

Commit

Permalink
add slice check in and delete function
Browse files Browse the repository at this point in the history
  • Loading branch information
phuhao00 committed Jul 17, 2022
1 parent 6c36229 commit 880c073
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
4 changes: 2 additions & 2 deletions map.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ func Values[K comparable, V any](in map[K]V) []V {
return result
}

func FiltrateBy[K comparable, V any](in map[K]V, fileter func(K, V) bool) map[K]V {
func FiltrateBy[K comparable, V any](in map[K]V, filtrate func(K, V) bool) map[K]V {
result := map[K]V{}

for k, v := range in {
if fileter(k, v) {
if filtrate(k, v) {
result[k] = v
}
}
Expand Down
27 changes: 25 additions & 2 deletions slice.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package sugar

func SliceFileter[V any](collection []V, fileter func(V, int) bool) []V {
import "golang.org/x/exp/constraints"

func SliceFiltrate[V any](collection []V, filtrate func(V, int) bool) []V {

result := []V{}

for i, v := range collection {
if fileter(v, i) {
if filtrate(v, i) {
result = append(result, v)
}
}
Expand Down Expand Up @@ -48,3 +50,24 @@ func SliceGroupBy[T any, U comparable](collection []T, iteratee func(T) U) map[U
}
return result
}

//CheckInSlice check value in slice
func CheckInSlice[T constraints.Ordered](a T, s []T) bool {
for _, val := range s {
if a == val {
return true
}
}
return false
}

//DelOneInSlice delete one element of slice left->right
func DelOneInSlice[T constraints.Ordered](a T, old []T) (new []T) {
for i, val := range old {
if a == val {
new = append(old[:i], old[i+1:]...)
return
}
}
return old
}

0 comments on commit 880c073

Please sign in to comment.