-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslices.go
49 lines (42 loc) · 1.03 KB
/
slices.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
package mango
func Map[F any, G any](list []F, mapper func(F) G) []G {
mapped := make([]G, len(list))
for i := range list {
mapped[i] = mapper(list[i])
}
return mapped
}
func Filter[F any](list []F, predicate func(F) bool) []F {
filtered := make([]F, 0, len(list))
for _, item := range list {
if predicate(item) {
filtered = append(filtered, item)
}
}
return filtered
}
func Find[F any](list []F, predicate func(F) bool) (F, bool) {
var empty F
for _, item := range list {
if predicate(item) {
return item, true
}
}
return empty, false
}
/******************************************************************************/
type Comparator[F any] func(F, F) bool
func SliceEqual[F any](first, second []F, comparator Comparator[F]) bool {
if len(first) != len(second) {
return false
}
for i := range first {
if !comparator(first[i], second[i]) {
return false
}
}
return true
}
func StringSliceEqual(first, second []string) bool {
return SliceEqual(first, second, func(a, b string) bool { return a == b })
}