Code Monkey home page Code Monkey logo

callbag-loop's Introduction

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)
  }),
)

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.