-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsource.go
62 lines (53 loc) · 1.64 KB
/
source.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
package jpipe
import (
"golang.org/x/exp/constraints"
)
// FromGoChannel creates a Channel from a Go channel
func FromGoChannel[T any](pipeline *Pipeline, channel <-chan T) *Channel[T] {
worker := func(node workerNode[any, T]) {
loopOverChannel(node, channel, func(value T) bool {
return node.Send(value)
})
}
_, output := newSourcePipelineNode("FromGoChannel", pipeline, worker)
return output
}
// FromSlice creates a Channel from a slice.
// All values in the slice are sent to the channel in order
func FromSlice[T any](pipeline *Pipeline, slice []T) *Channel[T] {
worker := func(node workerNode[any, T]) {
for _, value := range slice {
if !node.Send(value) {
return
}
}
}
_, output := newSourcePipelineNode("FromSlice", pipeline, worker)
return output
}
// FromRange creates a Channel from a range of integers.
// All integers between start and end (both inclusive) are sent to the channel in order
func FromRange[T constraints.Integer](pipeline *Pipeline, start T, end T) *Channel[T] {
worker := func(node workerNode[any, T]) {
for i := start; i <= end; i++ {
if !node.Send(i) {
return
}
}
}
_, output := newSourcePipelineNode("FromRange", pipeline, worker)
return output
}
// FromGenerator creates a Channel from a stateless generator function.
// Values returned by the function are sent to the channel in order.
func FromGenerator[T any](pipeline *Pipeline, generator func(i uint64) T) *Channel[T] {
worker := func(node workerNode[any, T]) {
for i := uint64(0); ; i++ {
if !node.Send(generator(i)) {
return
}
}
}
_, output := newSourcePipelineNode("FromGenerator", pipeline, worker)
return output
}