/
collection.go
44 lines (41 loc) · 1.31 KB
/
collection.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
package utils
// Partition splits the array of elements into two groups:
// the left partition contains elements that the predicate returns `true` for.
// the right partition contains elements that the predicate returns `false` for.
//
// The predicate is invoked with each element
func Partition[V any](elements []V, predicate func(V) bool) ([]V, []V) {
leftPartition := make([]V, 0, len(elements))
rightPartition := make([]V, 0, len(elements))
for _, v := range elements {
if predicate(v) {
leftPartition = append(leftPartition, v)
} else {
rightPartition = append(rightPartition, v)
}
}
return leftPartition, rightPartition
}
// Filter returns a new slice only containing the elements for which invoking the predicate
// returned true
//
// The predicate is invoked with each element
func Filter[V any](elements []V, predicate func(V) bool) []V {
filtered := make([]V, 0, len(elements))
for _, v := range elements {
if predicate(v) {
filtered = append(filtered, v)
}
}
return filtered
}
// Map returns a new slice where every value is mapped to a new value through the mapper
//
// The mapper is invoked with each element
func Map[V any, M any](elements []V, mapper func(V) M) []M {
mapped := make([]M, 0, len(elements))
for _, v := range elements {
mapped = append(mapped, mapper(v))
}
return mapped
}