Skip to content
Brian Cavalier edited this page Jun 17, 2016 · 4 revisions

Repeat the behavior of a stream N times.

import { empty, continueWith } from 'most'

// repeat :: number -> Stream a -> Stream a
// return a stream that repeats the behavior of the input
// stream n times. n must be >= 0.  When n === 0, the returned
// stream will be empty. When n === 1, the returned stream will
// be indistinguishable from the input stream.
const repeat = (n, stream) =>
  n <= 0 ? empty()
    : n === 1 ? stream
    : continueWith(() => repeat(n - 1, stream), stream)

Example

import { from } from 'most'

const s = from([1,2,3])

repeat(3, s)
  .observe(x => console.log(x)) //> 1 2 3 1 2 3 1 2 3