Skip to content

Latest commit

 

History

History
211 lines (142 loc) · 5.65 KB

chainable.md

File metadata and controls

211 lines (142 loc) · 5.65 KB

Chainable

Chainable provides generator functions for building chainable iterables.

The key difference between chainable and ChainableIterable is that chainable can be called as a function to start the chain. This can result in more compact notation.

Chainable As Function

Chainable can be called as a function to convert an iterable to a chainable iterable.

Parameters

  • iterable Iterable - iterable to make chainable

Examples

import { chainable } from 'iterablefu'
const a = chainable([1, 2, 3]).map(x => x * x).toArray()
console.log(a) // prints [1, 4, 9]

Returns ChainableIterable

Table of Contents

Generators

Static methods for creating ChainableIterables.

concatenate

Concatenates a list of iterables into a single iterable.

Parameters

  • iterables ...Iterable to be concatenated

Examples

const a = chainable.concatenate([0, 1, 2], [3, 4]).toArray()
console.log(a) // prints [0, 1, 2, 3, 4]

Returns ChainableIterable that provides the output of each iterable in turn

from

Creates a sequence from the input sequence. This function exists solely so that ChainableIterable has a static constructor method.

Parameters

  • iterable
  • inputIterable Iterable the iterable

Examples

const fn = function * (n) {
  for (i = 0; i < n; i++) {
    yield i
  }
}
const a = chainable.from(fn(5)).toArray()
console.log(a) // prints [0, 1, 2, 3, 4]

Returns ChainableIterable that represents the same iterable that was passed in

range

Creates a sequence of numbers similar to the Python range. See the example.

Parameters

  • args ...integer per the example

Examples

console.log([...chainable.range()]) // prints []
console.log([...chainable.range(5)]) // prints [0, 1, 2, 3, 4]
console.log([...chainable.range(2, 5)]) // prints [2, 3, 4, 5, 6]
console.log([...chainable.range(2, 5, 3)] // prints [2, 5, 8, 11, 14]

Returns ChainableIterable that represents the range

repeat

Generates a sequence of things, n times

Parameters

  • n number the number of times to repeat thing
  • thing any the repeated thing

Examples

const generator = chainable.repeat(5, 'a')
console.log([...generator]) // prints ['a', 'a', 'a', 'a', 'a']

Returns ChainableIterable that will repeat thing, n times

repeatIterable

Repeat an iterable n times.

NOTE: Generator functions are designed to create one-time-use iterables, and will not work as expected with repeatIterable. Once a generator completes, it won't restart, and therefore can't be repeated.

Instead, use an iterable object where calling [Symbol.iterator] returns a new Generator object with new state. See the examples below...

Parameters

  • n number number of times to repeat iterable
  • repeatableIterable Iterable the input iterable to repeat, see notes above and examples.

Examples

// As noted above, use functions that create iterable objects,
// not generator functions, with repeatIterable.
const repeatable = (length) => {
  return {
    * [Symbol.iterator] () {
      for (let i = 0; i < length; i++) {
        yield i
      }
    }
  }
}
const a = chainable.repeatIterable(3, repeatable(3))
console.log([...a]) // prints [0, 1, 2, 0, 1, 2, 0, 1, 2] as expected

// NOTE: This generator function will not work as expected with repeatIterable.
const oneTime = function * (length) {
  for (let i = 0; i < length; i++) {
    yield i
  }
}
const b = chainable.repeatIterable(3, oneTime(3))
console.log([...b]) // prints [0, 1, 2] OOPS!!!!

Returns ChainableIterable that will repeat the input iterable n times

zip

Creates a sequence of arrays the same length as the shortest iterable provided. The first array contains the first element from each of the iterables provided. The second array contains the second element from each of the iterables provided, and so on.

Parameters

  • iterables ...Iterable the iterables to be zipped

Examples

const a = [0, 1, 2]
const b = ['a', 'b', 'c', 'd', 'e', 'f'] // note that this array is longer than a
const generator = chainable.zip(a, b)
console.log([...generator]) // prints [[0, 'a'], [1, 'b'], [2, 'c']]

Returns ChainableIterable for the zipped arrays

zipAll

Creates a sequence of arrays the same length as the longest iterable provided. The first array contains the first element from each of the iterables provided. The second array contains the second element from each of the iterables provided, and so on. Missing elements from the shorter iterables are set to undefined.

Parameters

  • iterables ...Iterable the iterables to be zipped

Examples

const a = [0, 1, 2]
const b = ['a', 'b', 'c', 'd'] // note that this array is longer than a
const generator = chainable.zipAll(a, b)
console.log([...generator]) // prints [[0, 'a'], [1, 'b'], [2, 'c'], [undefined, 'd']]

Returns ChainableIterable for the zipped arrays