func main() {
// To make it work, we need four things. (definitions are in the README's glossary):
// 1) an input stream
input := Range(4) // ValueStream containing [0 1 2 3]
// 2) a stack of Transducers
transducers := []Transducer{Map(Inc), Filter(Even)} // increment then filter odds
// 3) a reducer to put at the bottom of the transducer stack
reducer := Append() // very simple reducer - just appends values into a []interface{}
// 4) a processor that puts it all together
result := Transduce(input, reducer, transducers...)
fmt.Println(result) // [2 4]
// Or, we can use the Go processor, which does the work in a separate goroutine
// and returns results through a channel.
// Make an input chan, and stream each value from Range(4) into it
in_chan := make(chan interface{}, 0)
go StreamIntoChan(Range(4), in_chan)
// Go provides its own bottom reducer (that's where it sends values out through
// the return channel). So we don't provide one - just the input channel. Note
// that we can safely reuse the transducer stack we declared earlier.
out_chan := Go(in_chan, 0, transducers...)
result2 := make([]interface{}, 0) // zero out the slice
for v := range out_chan {
result2 = append(result2, v)
}
fmt.Println(result) // [2 4]