Coder Social home page Coder Social logo

gocha's Introduction

GoCha

Build Status codecov GoDoc Go Report Card

Funny tools to play with channels.

Status

Currently under active development.

Components

  • Mux, A channel multiplexer. Gets a number of channels as input and returns a single channel as output. Each message sent on any of the input channel is forwarded to the output one.
  • Pipe, Takes a number of functions as input and exposes an input and output channels. Upon receiving a message on the input channel it applies all the input functions in order (the order is important) and sends the result on the output channel.
  • Tee, Takes a number of channels as output and exposes an input channel. Upon receiving a message on the input channel it forwards it to all the output channels.
  • Switch, Takes a number of channels as output and exposes an input channel. Using the provided function upon receiving a message on the input channel uses the function to decide what output channel to forward the message to.

Usage

Import package

import (
  "github.com/ganglio/gocha"
)

Mux

Initialize the muxer

m := gocha.NewMux()

Add few channels

c1 := make(chan interface{})
c2 := make(chan interface{})
c3 := make(chan interface{})

m.AddChannels(c1,c2,c3)

Profit

c1<-1
fmt.Println(<-m.Out())

Closing

When all the channels muxed into the Mux are closed the Mux is closed as well.

Pipe

Initialize the pipe

p := gocha.NewPipe()

Add few functions

f1 := func(i interface{}) interface{} { return 3 * i.(int) + 1}
f2 := func(i interface{}) interface{} { return i.(int) / 2}
f3 := func(i interface{}) interface{} { return i.(int) * i.(int)}

p.AddProcs(f1,f2,f3)

Profit

p.In()<-1
fmt.Println(<-p.Out())

Closing

When the input channel is closed the out is closed automatically

Tee

Initialize the tee

t := gocha.NewTee()

Add few outs

c1 := make(chan interface{})
c2 := make(chan interface{})
c3 := make(chan interface{})

t.AddOuts(c1,c2,c3)

Profit

t.In()<-1
fmt.Println(<-c1)
fmt.Println(<-c2)
fmt.Println(<-c3)

Closing

When the input channel is closed all the outs are closed

Switch

Initialize the Switch

s := gocha.NewTee(func(i interface{}) int {
  return i.(int)
})

Add few outs

c1 := make(chan interface{})
c2 := make(chan interface{})
c3 := make(chan interface{})

s.AddOuts(c1,c2,c3)

Profit

s.In()<-0
fmt.Println(<-c1)
s.In()<-1
fmt.Println(<-c2)
s.In()<-2
fmt.Println(<-c3)

Closing

When the input channel is closed all the outs are closed

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.