ez
ez
makes it easy to chain together stream operations.
This library declares a Stream
interface and implementations for finite, infinite sequences, as well as slices.
Usage
Convert a Seq or a Slice to a Stream
fibonacci := ez.InfiniteStream(func(yield func(int) bool) {
a := 0
if !yield(a) {
return
}
b := 1
for yield(b) {
a, b = b, b+a
}
})
oneToTen := ez.FiniteStream(func(yield func(int) bool) {
for i := range 10 {
if !yield(i + 1) {
return
}
}
})
sliceStream := ez.SliceStream([]int{1, 2, 3, 4, 5, 6, 7})
intStream := ez.Pipe(fibonacci,
ez.Filter(func(i int) bool { return i > 5 }),
ez.Map(func(i int) int { return i / 2 }),
ez.Skip[int](10),
ez.Take[int](10),
ez.Reverse[int],
)
float64Stream := ez.Convert(intStream, func(i int) float64 {
return math.Round(math.Sqrt(float64(i)))
})
values := ez.Collect(float64Stream)
concat := ez.Concat(
oneToTen,
sliceStream,
ez.Pipe(fibonacci, ez.Take[int](10)),
)
sum := ez.Reduce(concat, 0, func(acc int, v int) int { return acc + v })