Skip to content

Latest commit

 

History

History
196 lines (131 loc) · 5.45 KB

generators.md

File metadata and controls

196 lines (131 loc) · 5.45 KB

Generator Methods

Generator methods to create an iterable sequence from something else.

Usage

The IterableFu package exports the generators as an Object.

import { generators } from 'iterablefu'
console.log(...generators.range(5)) // prints 0 1 2 3 4

Table of Contents

concatenate

Concatenates a list of iterables into a single iterable.

Parameters

  • iterables ...Iterable to be concatenated

Examples

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

Returns Generator 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 generatorFunction = function * (n) {
  for (let i = 0; i < n; i++) {
    yield i
  }
}
const generator = from(generatorFunction(3))
console.log([...generator]) // prints [0, 1, 2]

Returns Generator 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([...range()]) // prints []

console.log([...range(5)]) // prints [0, 1, 2, 3, 4]

console.log([...range(2, 5)]) // prints [2, 3, 4, 5, 6]

console.log([...range(2, 5, 3)] // prints [2, 5, 8, 11, 14]

Returns Generator 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 = repeat(5, 'a')
console.log([...generator]) // prints ['a', 'a', 'a', 'a', 'a']

Returns Generator 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 iterable objects, not generator functions with repeatIterable
const repeatable = (length) => {
  return {
    * [Symbol.iterator] () {
      for (let i = 0; i < length; i++) {
        yield i
      }
    }
  }
}
const a = 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 = repeatIterable(3, oneTime(3))
console.log([...b]) // prints [0, 1, 2] OOPS!!!!

Returns Generator 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 = zip(a, b)
console.log([...generator]) // prints [[0, 'a'], [1, 'b'], [2, 'c']]

Returns Generator 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 = zipAll(a, b)
console.log([...generator]) // prints [[0, 'a'], [1, 'b'], [2, 'c'], [undefined, 'd']]

Returns Generator for the zipped arrays