-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstantiate.go
More file actions
129 lines (116 loc) · 3.94 KB
/
Copy pathinstantiate.go
File metadata and controls
129 lines (116 loc) · 3.94 KB
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
package iters
import (
"iter"
"maps"
"slices"
)
// Generate an infinite iter.Seq where each element is generated by the provided supplier function.
// Due to the potential statefulness of the supplier, multiple operations towards the same iter.Seq might provide
// different results.
//
// This creates a lazy sequence. The supplier won't be called until it is requested for consumption by
// the transformation or aggregation functions.
//
// Caution! Unless you limit the length of the iter.Seq (e.g. using the Limit function), the aggregation
// functions over this iter.Seq might not terminate.
func Generate[T any](supplier func() T) iter.Seq[T] {
return func(yield func(T) bool) {
for yield(supplier()) {
}
}
}
// Iterate returns an infinite sequential ordered iter.Seq produced by iterative application of a function
// f to an initial element seed, producing a iter.Seq consisting of seed, f(seed), f(f(seed)), etc.
// The first element (position 0) in the iter.Seq will be the provided seed. For n > 0, the element at
// position n, will be the result of applying the function f to the element at position n - 1.
// Due to the stateful nature of the supplier, multiple operations towards the same iter.Seq might provide
// different results.
func Iterate[T any](seed T, f func(T) T) iter.Seq[T] {
return func(yield func(T) bool) {
lastElement := seed
for yield(lastElement) {
lastElement = f(lastElement)
}
}
}
// Concat creates a lazily concatenated iter.Seq whose elements are all the elements of the first
// provided iter.Seq followed by all the elements of the second provided iter.Seq, followed by the
// elements of the third iter.Seq (if any), and so on.
func Concat[T any](seqs ...iter.Seq[T]) iter.Seq[T] {
return func(yield func(T) bool) {
for _, seq := range seqs {
for i := range seq {
if !yield(i) {
return
}
}
}
}
}
// Empty returns an empty iter.Seq
func Empty[T any]() iter.Seq[T] {
return func(_ func(T) bool) {}
}
// Keys returns an iter.Seq that iterates the keys (first/left items) of the source iter.Seq2
func Keys[K, V any](source iter.Seq2[K, V]) iter.Seq[K] {
return func(yield func(K) bool) {
for k := range source {
if !yield(k) {
return
}
}
}
}
// Values returns an iter.Seq that iterates the values (second/right items) of the source iter.Seq2
func Values[K, V any](source iter.Seq2[K, V]) iter.Seq[V] {
return func(yield func(V) bool) {
for _, v := range source {
if !yield(v) {
return
}
}
}
}
// Of returns an iter.Seq that iterates each of the passed arguments in order.
func Of[T any](a ...T) iter.Seq[T] {
return slices.Values(a)
}
// OfRange returns an iter.Seq that iterates the integer numbers from
// the first argument (inclusive) to the second argument (exclusive).
func OfRange[T integer](start, excludedEnd T) iter.Seq[T] {
return func(yield func(T) bool) {
for i := start; i < excludedEnd; i++ {
if !yield(i) {
return
}
}
}
}
// OfChannel returns an iter.Seq that iterates over all values received from the provided channel.
// The iteration continues until the channel is closed.
// This means that aggregations over the returned iter.Seq might not terminate if the channel
// is inactive or never closed.
func OfChannel[T any](ch <-chan T) iter.Seq[T] {
return func(yield func(T) bool) {
for i := range ch {
if !yield(i) {
return
}
}
}
}
// OfSlice returns an iter.Seq that iterates over all elements of the provided slice.
// It is just an alias for slices.Values.
func OfSlice[T any](sl []T) iter.Seq[T] {
return slices.Values(sl)
}
// OfMapKeys returns an iter.Seq that iterates over all keys of the provided map.
// It is just an alias for maps.Keys.
func OfMapKeys[T comparable, K any](m map[T]K) iter.Seq[T] {
return maps.Keys(m)
}
// OfMapValues returns an iter.Seq that iterates over all values of the provided map.
// It is just an alias for maps.Values.
func OfMapValues[T comparable, V any](m map[T]V) iter.Seq[V] {
return maps.Values(m)
}