-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmap.go
37 lines (24 loc) · 797 Bytes
/
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
package stream
import "golang.org/x/exp/constraints"
type MapAction[I, O any] func(I) O
func Map[I, O any](input chan I, action MapAction[I, O]) *BaseStream[O] {
output := make(chan O)
go mapRun(input, output, action)
return NewBase[O](output)
}
func MapComparable[I any, O comparable](input chan I, action MapAction[I, O]) *ComparableStream[O] {
output := make(chan O)
go mapRun(input, output, action)
return NewComparable(output)
}
func MapOrdered[I any, O constraints.Ordered](input chan I, action MapAction[I, O]) *OrderedStream[O] {
output := make(chan O)
go mapRun(input, output, action)
return NewOrdered(output)
}
func mapRun[I, O any](input <-chan I, output chan<- O, action MapAction[I, O]) {
for elem := range input {
output <- action(elem)
}
close(output)
}