-
Notifications
You must be signed in to change notification settings - Fork 0
/
iterable.go
46 lines (42 loc) · 1.09 KB
/
iterable.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
// iterable is an interface that represents an iterable collection of elements.
package iterable
// iterable is an interface that represents an iterable collection of elements.
type Iterable[K comparable, V any] interface {
Next() (K, V, bool)
HasNext() bool
Reset()
}
// ToSlice is a function that converts an iterable collection to a slice.
func ToSlice[V any](i Iterable[int, V]) []V {
var slice []V
for i.HasNext() {
_, v, _ := i.Next()
slice = append(slice, v)
}
return slice
}
// ToMap is a function that converts an iterable collection to a map.
func ToMap[K comparable, V any](i Iterable[K, V]) map[K]V {
var m map[K]V
for i.HasNext() {
k, v, _ := i.Next()
m[k] = v
}
return m
}
// Map is a function that converts an iterable collection to new iterable collection with given function.
func Map[K, NK comparable, V, NV any](i Iterable[K, V], fn func(K, V) (NK, NV)) Iterable[NK, NV] {
length := 0
for i.HasNext() {
i.Next()
length++
}
i.Reset()
m := make(map[NK]NV, length*4/3+1)
for i.HasNext() {
k, v, _ := i.Next()
nk, nv := fn(k, v)
m[nk] = nv
}
return FromMap(m)
}