2.0.0 • Published 6 years ago

callbag-loop v2.0.0

Weekly downloads
5
License
MIT
Repository
github
Last release
6 years ago

callbag-loop

Callbag operator that accumulates results using a feedback loop that emits one value and feeds back another to be used in the next iteration.

It allows you to maintain and update a “state” (a.k.a. feedback, a.k.a. seed for the next iteration) while emitting a different value. In contrast, scan feeds back and produces the same value.

Inspired by most.js's loop.

Example

import forEach from 'callbag-for-each'
import fromIter from 'callbag-from-iter'
import loop from 'callbag-loop'
import pipe from 'callbag-pipe'

const add = (a, b) => a + b
const average = values => values.reduce(add, 0) / values.length

pipe(
  fromIter([10, 20, 60, 20, 5, 150, 3, 80]),
  loop((seed, value) => {
    seed.push(value)
    seed = seed.slice(-4)
    return [seed, average(seed)]
  }, []),
  forEach(avg => {
    // will log 10, 15, 30, 27.5, 26.25, 58.75, 44.5, 59.5
    console.log(avg)
  }),
)
2.0.0

6 years ago

1.0.0

6 years ago