-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathfilter.go
75 lines (61 loc) · 1.94 KB
/
filter.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package gogu
// Filter returns all the elements from the collection which satisfies the conditional logic of the callback function.
func Filter[T any](slice []T, fn func(T) bool) []T {
res := make([]T, 0)
for _, v := range slice {
if fn(v) {
res = append(res, v)
}
}
return res
}
// Reject is the opposite of Filter.
// It returns the values from the collection without the elements for which the callback function returns true.
func Reject[T any](slice []T, fn func(val T) bool) []T {
// TODO considering to create a new slice and append the values resulted
// from the callback function, even if this imply a new allocation.
for i := 0; i < len(slice); i++ {
if fn(slice[i]) {
slice = append(slice[:i], slice[i+1:]...)
i--
}
}
return slice
}
// FilterMap iterates over the elements of a collection and returns a new collection
// representing all the items which satisfies the criteria formulated in the callback function.
func FilterMap[K comparable, V any](m map[K]V, fn func(V) bool) map[K]V {
filtered := map[K]V{}
for k, v := range m {
if fn(v) {
filtered[k] = v
}
}
return filtered
}
// FilterMapCollection filter out a one dimensional collection of map items
// by applying the conditional logic of the callback function.
func FilterMapCollection[K comparable, V any](collection []map[K]V, fn func(V) bool) []map[K]V {
filtered := []map[K]V{}
for _, item := range collection {
for _, v := range item {
if fn(v) {
filtered = append(filtered, item)
}
}
}
return filtered
}
// Filter2DMapCollection filter out a two-dimensional collection of map items
// by applying the conditional logic of the callback function.
func Filter2DMapCollection[K comparable, V any](collection []map[K]map[K]V, fn func(map[K]V) bool) []map[K]map[K]V {
filtered := []map[K]map[K]V{}
for _, item := range collection {
for _, v := range item {
if fn(v) {
filtered = append(filtered, item)
}
}
}
return filtered
}