-
Notifications
You must be signed in to change notification settings - Fork 1
/
pipe.go
31 lines (25 loc) · 899 Bytes
/
pipe.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
package stream
import "github.com/primetalk/goio/fun"
// Pipe is a conversion of one stream to another.
// Technically it's a function that takes one stream and returns another.
type Pipe[A any, B any] func(Stream[A]) Stream[B]
// Sink is a pipe that does not return meaningful values.
type Sink[A any] Pipe[A, fun.Unit]
// Through passes the stream data through the pipe.
// Technically it applies the pipe function to this stream.
func Through[A any, B any](stm Stream[A], pipe Pipe[A, B]) Stream[B] {
return pipe(stm)
}
// NewSink constructs the sink from the provided function.
func NewSink[A any](f func(a A)) Sink[A] {
return func(stm Stream[A]) Stream[fun.Unit] {
return Map(stm, func(a A) fun.Unit {
f(a)
return fun.Unit1
})
}
}
// ToSink streams all data from the stream to the sink.
func ToSink[A any](stm Stream[A], sink Sink[A]) Stream[fun.Unit] {
return sink(stm)
}