Skip to content

Commit 880c073

Browse files
committed
add slice check in and delete function
1 parent 6c36229 commit 880c073

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed

map.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ func Values[K comparable, V any](in map[K]V) []V {
2020
return result
2121
}
2222

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

2626
for k, v := range in {
27-
if fileter(k, v) {
27+
if filtrate(k, v) {
2828
result[k] = v
2929
}
3030
}

slice.go

+25-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package sugar
22

3-
func SliceFileter[V any](collection []V, fileter func(V, int) bool) []V {
3+
import "golang.org/x/exp/constraints"
4+
5+
func SliceFiltrate[V any](collection []V, filtrate func(V, int) bool) []V {
46

57
result := []V{}
68

79
for i, v := range collection {
8-
if fileter(v, i) {
10+
if filtrate(v, i) {
911
result = append(result, v)
1012
}
1113
}
@@ -48,3 +50,24 @@ func SliceGroupBy[T any, U comparable](collection []T, iteratee func(T) U) map[U
4850
}
4951
return result
5052
}
53+
54+
//CheckInSlice check value in slice
55+
func CheckInSlice[T constraints.Ordered](a T, s []T) bool {
56+
for _, val := range s {
57+
if a == val {
58+
return true
59+
}
60+
}
61+
return false
62+
}
63+
64+
//DelOneInSlice delete one element of slice left->right
65+
func DelOneInSlice[T constraints.Ordered](a T, old []T) (new []T) {
66+
for i, val := range old {
67+
if a == val {
68+
new = append(old[:i], old[i+1:]...)
69+
return
70+
}
71+
}
72+
return old
73+
}

0 commit comments

Comments
 (0)