go-streams

MIT License

Stars
5
Committers
1

go-streams

go-streams is a library which provides a stream mechanism based on Java 8's Stream API. A Stream is a lazily evaluated chain of functions which operates on some source of values. Streams allows you to define a pipeline of operations to perform on a source of iterated values. The pieces of the pipeline are lazily evaluated, so for example, items which fail a Filter operation will not be passed to a following Map operation (or any operation).

This library does not depend on any other packages at runtime - all dependencies are for tests only.

Use this library with:

import "github.com/DemonWav/go-streams"

A typical usage of this API might look something like this:

package main

import (
        "fmt"
        "github.com/DemonWav/go-streams"
)

func main() {
        cancel := make(chan bool, 1)
        c := make(chan int)

        go func() {
                for i := 2; ; i++ {
                        select {
                        case c <- i:
                        case <-cancel:
                                close(c)
                                return
                        }
                }
        }()

        seen := make([]int, 0)

        streams.NewStream(c).
                WithCancel(cancel).
                Filter(func(i int) bool {
                        return streams.NewStream(seen).None(func(n int) bool {
                                return i%n == 0
                        })
                }).
                OnEach(func(i int) {
                        seen = append(seen, i)
                }).
                Take(100).
                ForEach(func(i int) {
                        fmt.Println(i)
                })
}

In this example, an infinite Stream of integers is used as a channel source for the Stream, and the first 100 prime numbers are printed using the Sieve of Eratosthenes. The number of total prime numbers output can be controlled simply by modifying the argument to the Take method.

Read the docs for in-depth information on how to use this library.

Package Rankings
Top 7.95% on Proxy.golang.org
Badges
Extracted from project README
Build Status GoDoc