forked from OneOfOne/genh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
filters.go
82 lines (70 loc) · 1.79 KB
/
filters.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
76
77
78
79
80
81
82
package genh
func GroupBy[M ~map[MapKey]MapVal, GM map[MapKey][]MapVal, MapKey comparable, MapVal any](in M, fn func(k MapKey, v MapVal) MapKey) (out GM) {
out = make(GM)
for k, v := range in {
gk := fn(k, v)
out[gk] = append(out[gk], v)
}
for k := range out {
out[k] = Clip(out[k])
}
return
}
// Filter filters a slice optionally in place.
func Filter[S ~[]E, E any](in S, f func(E) (keep bool), inplace bool) (out S) {
if inplace {
out = in[:0]
} else {
out = make(S, 0, len(out))
}
for _, v := range in {
if f(v) {
out = append(out, v)
}
}
return Clip(out)
}
// SliceMap takes a slice of type E, calls fn on each value of `in` and returns the results as a slice of type `T`
func SliceMap[S ~[]E, E, T any](in S, fn func(E) T) []T {
out := make([]T, 0, len(in))
for _, v := range in {
out = append(out, fn(v))
}
return Clip(out)
}
// SliceMap takes a slice of type E, calls fn on each value of `in` and returns the modified in or a copy of it
func SliceMapSameType[S ~[]E, E any](in S, fn func(E) E, inplace bool) (out S) {
if inplace {
out = in[:0]
} else {
out = make(S, 0, len(out))
}
for _, v := range in {
out = append(out, fn(v))
}
return Clip(out)
}
// SliceMapFilter merged SliceMap and Filter
func SliceMapFilter[S ~[]E, E, T any](in S, fn func(E) (val T, ignore bool)) []T {
out := make([]T, 0, len(in))
for _, v := range in {
if nv, ignore := fn(v); !ignore {
out = append(out, nv)
}
}
return Clip(out)
}
// SliceMapFilter merged SliceMapSameType and Filter
func SliceMapFilterSameType[S ~[]E, E any](in S, fn func(E) (val E, ignore bool), inplace bool) (out S) {
if inplace {
out = in[:0]
} else {
out = make(S, 0, len(out))
}
for _, v := range in {
if nv, ignore := fn(v); !ignore {
out = append(out, nv)
}
}
return Clip(out)
}