-
Notifications
You must be signed in to change notification settings - Fork 1
/
iter.go
188 lines (172 loc) · 3.26 KB
/
iter.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package go_iter
/*
TODO:
- From_file().
- From_url()
- GroupBy()
*/
// Clears iterator if not all the elements of the iteraor have been consumed
func Clear[T any](r chan T) {
for _ = range r {
}
}
// Models a pair {index, Value}
type Pair[A, B any] struct {
First A
Second B
}
// Creates an Iterable (channel) from a Slice / Array of data of type [T]
func Iterable_from_Array[T any](array []T) chan T {
out := make(chan T)
go func() {
defer close(out)
for _, x := range array {
out <- x
}
}()
return out
}
type Generator[U any] interface {
Next()
HasNext() bool
Value() U
}
// Returns the values from a generator via a channel
func Generator_to_Iterator[U any](c Generator[U]) chan U {
ch := make(chan U)
go func() {
defer close(ch)
for c.HasNext() {
ch <- c.Value()
c.Next()
}
}()
return ch
}
// Maps input channel in to output channel out using callback
func Map[A, B any](ch1 chan A, f func(A) B) chan B {
ch2 := make(chan B)
go func() {
defer close(ch2)
for x := range ch1 {
ch2 <- f(x)
}
}()
return ch2
}
// Filters input channel in to output channel out using callback
func Filter[T any](ch1 chan T, f func(T) bool) chan T {
ch2 := make(chan T)
go func() {
defer close(ch2)
for x := range ch1 {
if f(x) {
ch2 <- x
}
}
}()
return ch2
}
// Reduces a list of values to a single value (functional)
func Reduce[T any](ch1 chan T, f func(T, T) T) T {
var temp T
for x := range ch1 {
temp = f(temp, x)
}
return temp
}
// Every : Take every in N item from input channel (backpressure management)
// Ex: Every(in, 2) takes every second item from 'in, put sit into into 'out'
func Every[T any](in chan T, n int) chan T {
out := make(chan T)
go func() {
defer close(out)
index := 0
for x := range in {
if index%n == 0 {
out <- x
index = 0
}
index++
}
}()
return out
}
// Skips next N items in a list
func Skip[T any](in chan T, n int) chan T {
out := make(chan T)
go func() {
defer close(out)
index := 0
n++
for x := range in {
if index == n || index == 0 {
out <- x
index = 0
}
index++
}
}()
return out
}
// Takes the 'nmax' first entries form 'in'
func Take[T any](in chan T, nmax int) chan T {
out := make(chan T)
go func() {
defer close(out)
index := 0
for i := range in {
index++
if index <= nmax {
out <- i
} else {
break
}
}
}()
return out
}
// Takes a slice [nmin, nmax] from 'in' into 'out'
func Slice[T any](in chan T, nmin, nmax int) chan T {
out := make(chan T)
go func() {
defer close(out)
index := 0
for i := range in {
if index < nmin {
index++
continue
} else if index <= nmax && index >= nmin {
out <- i
} else if index > nmax {
break
}
index++
}
}()
return out
}
// Lists the elements from 'in' into 'out' with an index (as a 'Pair')
func Enumerate[T any](in chan T) chan Pair[int, T] {
out := make(chan Pair[int, T])
go func() {
defer close(out)
index := 0
for i := range in {
out <- Pair[int, T]{index, i}
index++
}
}()
return out
}
// Iterable channel of integers < nmax: Emulates 'range(nmax)' from Python
func Range(nmax int) chan int {
out := make(chan int)
go func() {
defer close(out)
for index := 0; index < nmax; index++ {
out <- index
}
}()
return out
}