Skip to content
No description, website, or topics provided.
Go
Branch: master
Clone or download
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.gitignore
channels.go Improve performance by removing unnecessary copies Feb 23, 2019
go.mod Initial commit Feb 21, 2019
go.sum Initial commit Feb 21, 2019
license.txt Add license Feb 22, 2019
readme.md Simplify constructor by merging chan and slice sources Feb 25, 2019
slices.go Add Reduce functionality Feb 23, 2019
stream.go Simplify constructor by merging chan and slice sources Feb 25, 2019
stream_test.go Simplify constructor by merging chan and slice sources Feb 25, 2019

readme.md

go-streams

Build Status GoDoc Current Release

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.

You can’t perform that action at this time.