Giter Club home page Giter Club logo

chanpipes's Introduction

chanpipes

-- import "github.com/dkrieger/chanpipes"

chanpipes provides helpers for building networks of channels, using POSIX shell pipeline -like semantics where applicable.

Rob Pike makes repeated comparisons between POSIX shell semantics and Go channel/goroutine semantics in his seminal "Go Concurrency Patterns" talk (https://talks.golang.org/2012/concurrency.slide#1). Goroutines are like background processes, and channels are like named pipes (fifos); chanpipes takes its name from the latter analogy.

Note that, much as shell pipelines use text as the universal interface, chanpipes pipelines use interface{} for maximum interoperability, at the expense of runtime safety. If/when golang gets generic types, some runtime safety may be restored; in the meantime, our POSIX shell analogy is even more literal, and the same approach of validating inputs at runtime should be taken. If generic types don't come with golang 2, code generation may be used to implement strongly-typed pipelines.

Usage

func Cat

func Cat(inputs ...<-chan interface{}) <-chan interface{}

Cat is like a dynamic "select" statement for N readable channels. It is a fan-in pattern, which is like "cat" in a POSIX shell. A notable difference between Cat and cat is that Cat doesn't care what order input chans are passed, and will forward messages to the output chan in the order they arrive

func Grep

func Grep(in <-chan interface{}, cond func(interface{}) bool) (<-chan interface{}, <-chan interface{})

Grep is a filtering operation. Unlike the behavior of "grep", which filters each line of stdin independently, Grep filters all of "stdin". It's more like

# mkfifo foo bar pass fail && <foo cat >bar &
# <input tee foo | grep condition >/dev/null 2>&1 && <bar cat >pass || <bar cat >fail &

than

# mkfifo output
# <input grep condition >output &

func New

func New() (<-chan interface{}, chan<- interface{})

New creates a new channel and returns a read-only reference ("out") and a write-only reference ("in")

func Pipe

func Pipe(in <-chan interface{}, mapper func(interface{}) interface{}) <-chan interface{}

Pipe takes some readable channel as input and transforms its contents using an interface{}-to-interface{} mapper writing results to the new readable "out" channel.

func Tee

func Tee(in <-chan interface{}) (<-chan interface{}, <-chan interface{})

Tee takes some readable channel as input, forwards it to a new readable channel ("out"), then forwards it to another new readable channel ("side") after the goroutine wakes up. The idea is to read the final "out" before any "side", ensuring every side channel actually gets wired.

chanpipes's People

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.