-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmap.go
104 lines (94 loc) · 2.18 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
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package iterable
import (
"github.com/peterzeller/go-fun/slice"
"github.com/peterzeller/go-fun/zero"
)
func Map[A, B any](base Iterable[A], f func(A) B) Iterable[B] {
return &mapIterable[A, B]{base, f}
}
func MapIterator[A, B any](base Iterator[A], f func(A) B) Iterator[B] {
return &mapIterator[A, B]{base, f}
}
type mapIterable[A, B any] struct {
base Iterable[A]
f func(A) B
}
// Length of a mapIterable is the same as the length of the base
func (i mapIterable[A, B]) Length() int {
return Length(i.base)
}
type mapIterator[A, B any] struct {
base Iterator[A]
f func(A) B
}
func (i mapIterable[A, B]) Iterator() Iterator[B] {
return &mapIterator[A, B]{i.base.Iterator(), i.f}
}
func (i *mapIterator[A, B]) Next() (B, bool) {
if a, ok := i.base.Next(); ok {
return i.f(a), true
}
var b B
return b, false
}
func FlatMap[A, B any](base Iterable[A], f func(A) Iterable[B]) Iterable[B] {
return IterableFun[B](func() Iterator[B] {
it := base.Iterator()
var current Iterator[B]
return Fun[B](func() (B, bool) {
for {
if current == nil {
a, ok := it.Next()
if !ok {
return zero.Value[B](), false
}
current = f(a).Iterator()
}
b, ok := current.Next()
if ok {
return b, true
}
current = nil
}
})
})
}
func FlatMapBreadthFirst[A, B any](base Iterable[A], f func(A) Iterable[B]) Iterable[B] {
return IterableFun[B](func() Iterator[B] {
it := base.Iterator()
firstPass := true
var iterators []Iterator[B]
pos := 0
return Fun[B](func() (B, bool) {
for {
if !firstPass && len(iterators) == 0 {
return zero.Value[B](), false
}
if pos >= len(iterators) {
if firstPass {
// get next element from base iterator
i, ok := it.Next()
if ok {
iterators = append(iterators, f(i).Iterator())
} else {
// no more element in base iterator
firstPass = false
pos = 0
continue
}
} else {
pos = 0
continue
}
}
r, ok := iterators[pos].Next()
if ok {
pos++
return r, true
}
// remove iterator from iterators list and try with next position
iterators = slice.RemoveAt(iterators, pos)
}
})
})
}