@basic-streams/scan

scan operator for basic-streams

Usage no npm install needed!

<script type="module">
  import basicStreamsScan from 'https://cdn.skypack.dev/@basic-streams/scan';
</script>

README

@basic-streams/scan

scan<N, A>(
  reducer: (accumulated: A, next: N) => A,
  seed: A,
  stream: Stream<N>,
): Stream<A>

Creates a stream containing reducer(a, x) for each value x from the source stream, and the latest value a produced from by the resulting stream. The resulting stream will also have given seed as the first event.

import ofMany from "@basic-streams/of-many"
import scan from "@basic-streams/scan"

const stream = ofMany([1, 2, 3], 5000)

const result = scan((acc, next) => acc + next, 0, stream)

result(x => {
  console.log(x)
})

// > 0
// > 1
// > 3
// > 6

// stream: ____1____2____3
// result: 0___1____3____6