/reactive-channel

reactive programing utilities for go

Primary LanguageGoMIT LicenseMIT

reactive-channel

Reactive programming utilities for go, implmenting with go channels. Inspired by ReactiveX.

install

go get -u github.com/vagase/reactive-channel

example

Distinct

inChan := From([]interface{}{1, 2, 2, 3, 3, 5})
outChan := Distinct(inChan)

outChan will emit: 1, 2, 3, 5

Filter

inChan := From([]interface{}{1, 2, 3, 4})
outChan := Filter(inChan, func(i interface{}) bool {
  num := i.(int)
  return num%2 == 0
})

outChan will emit: 2 4

CombineLatest

// 1, 2, 3, 4
inChan1 := IntervalRange(1, 4, time.Millisecond*30, 0)
// a, b
inChan2 := Map(IntervalRange(0, 2, time.Millisecond*50, 0), func(i interface{}) interface{} {
  return string(97 + i.(int))
})
outChan := Map(CombineLatest(inChan1, inChan2), func(i interface{}) interface{} {
  values := i.([]interface{})
  return strconv.Itoa(values[0].(int)) + values[1].(string)
})

outChan will emit: "1a", "2a", "3a", "3b", "4b"

Debounce

inChan := IntervalRange(0, 4, 40*time.Millisecond, 0)
outChan := Debounce(inChan, time.Millisecond*100)

outChan will emit: 0, 3

usage

reactive-channel implements most useful ReactiveX operators, checkout full api list here.

license

MIT Licence © vagase