-
Notifications
You must be signed in to change notification settings - Fork 0
/
map.go
55 lines (49 loc) · 1.37 KB
/
map.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
package iterator
import (
"fmt"
)
// Map returns a modifier that applies fn on each item from the iterator
// It will replace the item with the value returned from fn
func Map[T any, S any](fn func(int, T) (S, error)) Modifier[T, S] {
return FilterMap(func(i int, item T) (S, bool, error) {
nItem, err := fn(i, item)
if err != nil {
return *new(S), false, err
}
return nItem, true, nil
})
}
// Replace returns a modifier that changes occurances of old with new
// for as many times
func Replace[T comparable](old T, new T, times int) Modifier[T, T] {
return func(iter Iterator[T]) Iterator[T] {
var count int
return Map(func(_ int, item T) (T, error) {
if count < times && item == old {
count++
return new, nil
}
return item, nil
})(iter)
}
}
// ReplaceAll returns a modifier that changes all occurances of old with new
func ReplaceAll[T comparable](old T, new T) Modifier[T, T] {
return Map(func(_ int, item T) (T, error) {
if item == old {
return new, nil
}
return item, nil
})
}
// Strings is a modifier that converts all items to strings
func Strings[T any](iter Iterator[T]) Iterator[string] {
return Map(func(_ int, item T) (string, error) {
return fmt.Sprint(item), nil
})(iter)
}
func ToSlices[T any](iter Iterator[Iterator[T]]) Iterator[[]T] {
return Map(func(_ int, it Iterator[T]) ([]T, error) {
return ToSlice(it)
})(iter)
}