-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathiterator.go
47 lines (42 loc) · 917 Bytes
/
iterator.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
package headwater
func ForEach[T any](array []T, callback func(item T)) {
for _, item := range array {
callback(item)
}
}
func Map[T any, U any](array []T, callback func(item T) U) []U {
var output []U = make([]U, len(array))
for index, item := range array {
result := callback(item)
output[index] = result
}
return output
}
func Reduce[T any, U any](array []T, callback func(target U, item T) U, target U) U {
var output U = target
for _, item := range array {
output = callback(output, item)
}
return output
}
func Filter[T any](array []T, callback func(item T) bool) []T {
var output []T = make([]T, 0)
for _, item := range array {
valid := callback(item)
if valid {
output = append(output, item)
}
}
return output
}
func Equal[T comparable](a, b []T) bool {
if len(a) != len(b) {
return false
}
for i, v := range a {
if v != b[i] {
return false
}
}
return true
}