/iter

Lazily evaluated utility library

Primary LanguageJavaScript

iter Build Status

Utility library for functional programming based on ES2015 generators that ensures lazy evaluation of possibly infinite ranges.

Examples

Fizzbuzz generator

import { compose, map, range, take } from 'iter';

const fizzBuzz = compose(
  map(n => n % 3 === 0 ? 'fizz' : n),
  map(n => n % 5 === 0 ? 'buzz' : n),
  map(n => n % 5 === 0 && n % 3 === 0 ? 'fizzbuzz' : n),
  range(1, Infinity)
);

[...take(15, fizzBuzz)]
  // => [1, 2, 'fizz', 4, 'buzz', 'fizz', 7, 8 , 'fizz', 'buzz', 11, 'fizz', 13, 14, 'fizzbuzz']

Fibonacci sequence up to the nth number

import { zipWith, tail, take } from 'iter';

const fibonacci = function * () {
  yield 0;
  yield 1;
  yield * zipWith((x, y) => x + y, fibonacci(), tail(fibonacci()));
};

[...take(8, fibonacci())]
  // => [0, 1, 1, 2, 3, 5, 8, 13]

Methods

  • assertIterable(iterable)
  • compact(iterable)
  • compose(...iterables)
  • filter(filterFn, iterable)
  • isIterable(iterable)
  • map(mapFn, iterable)
  • pluck(iterable)
  • slice(fromIdx, len, iterable)
  • tail(iterable)
  • take(num, iterable)
  • unqiue(iterable)
  • zipWith(zipFn, ...iterables)

Inspiration & further reading: