Skip to content

Latest commit

 

History

History
600 lines (402 loc) · 15.9 KB

File metadata and controls

600 lines (402 loc) · 15.9 KB

ChainableIterable

The ChainableIterable implements chaining for the IterableFu package. Typically, instances are created using chainable instead of creating ChainableIterable directly.

import { chainable, ChainableIterable } from 'iterablefu'
const chainableIterable = chainable([1, 2, 3])
console.log(chainableIterable instanceof ChainableIterable) // prints true

There is a static constructor method that converts an iterable (including Generator objects) to a chainable iterable.

import { ChainableIterable } from 'iterablefu'
const a = ChainableIterable.from([1, 2, 3]).toArray()
console.log(a) // prints [1, 2, 3]

There is a constructor that converts an iterable to a chainable iterable.

import { ChainableIterable } from 'iterablefu'
const a = new ChainableIterable([1, 2, 3]).toArray()
console.log(a) // prints [1, 2, 3]

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 = ChainableIterable.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 = ChainableIterable.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([...ChainableIterable.range()]) // prints []
console.log([...ChainableIterable.range(5)]) // prints [0, 1, 2, 3, 4]
console.log([...ChainableIterable.range(2, 5)]) // prints [2, 3, 4, 5, 6]
console.log([...ChainableIterable.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 = ChainableIterable.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 = ChainableIterable.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 = ChainableIterable.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 = ChainableIterable.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 = ChainableIterable.zipAll(a, b)
console.log([...generator]) // prints [[0, 'a'], [1, 'b'], [2, 'c'], [undefined, 'd']]

Returns ChainableIterable for the zipped arrays

Transforms

Methods for transforming a sequence.

arrayToObject

Converts a sequence of Arrays to a sequence of Objects by assigning the property names to each array element in turn. The input sequence doesn't have to provide arrays, it can provide any sequence of iterable objects.

If the arrays are too long, extra values are ignored.

If the arrays are too short, the remaining properties are assigned undefined.

Parameters

  • propertyNames Iterable a sequence of property names
  • iterable Iterable a sequence of arrays (or any iterable objects)

Examples

const input = [[0, 1], [2, 3, 'a'], [4]]
const a = ChainableIterable.from(input).arrayToObject(['a', 'b']).toArray()
console.log(a) // prints [{'a': 0, 'b': 1 }, {'a': 2, 'b': 3 }, {'a': 4, 'b': undefined }]

Returns ChainableIterable for the sequence of Objects

chunk

Chunk every n items into an array, and output that array in the output sequence.

Parameters

  • n number the number of items to group into each array.
  • iterable Iterable the sequence of items to group

Examples

const input = [0, 1, 2, 3, 4, 5, 6]
const a = ChainableIterable.from(input).chunk(2).toArray()
console.log(a) // prints [[0, 1], [2, 3], [4, 5], [6]]

Returns ChainableIterable for the chunked sequence

diff

Execute fn(previous, current) and yields the result for each pair. Would be useful for calculating time differences between timestamps.

Parameters

  • fn Function fn(previous, current), yielding return value
  • iterable Iterable the input iterable

Examples

const input = [0, 1, 2, 3, 4]
const a = ChainableIterable.from(input).diff((n, m) => m - n).toArray()
console.log(a)  // prints [1, 1, 1, 1]

Returns ChainableIterable if input has two or more items, output sequence is one shorter than input sequence. Otherwise, no items are output.

filter

Keeps item from input sequence when fn(item) returns truthy. Remove items from input sequence when fn(item) returns !truthy.

Parameters

  • fn Function fn(item) returns truthy when item should be removed
  • iterable Iterable the sequence to filter

Examples

const isEvenNumber = x => x % 2 === 0
const input = [0, 1, 2, 3, 4, 5, 6]
const a = ChainableIterable.from(input).filter(isEvenNumber).toArray()
console.log(a) // prints even numbers [0, 2, 4, 6]

Returns ChainableIterable for the filtered sequence

flatten

Flattens a sequence of items one level deep. It does not flatten strings, even though they are iterable.

Parameters

  • iterable Iterable the iterable sequence to flatten

Examples

const input = [[0, 1], [2, 3], [4, 5], [6]]
const a = ChainableIterable.from(input).flatten().toArray()
console.log(a) // prints [0, 1, 2, 3, 4, 5, 6]

Returns ChainableIterable for the flattened sequence

flattenRecursive

Flattens a sequence by recursively returning items from each iterable in the sequence. Does not flatten strings even though they are iterable.

Parameters

  • iterable Iterable the sequence to flatten

Examples

const input = [0, [1, 2, 3], [[4, 5], [[[6, 7]], [8, 9], 10]], 11, 12]
const a = ChainableIterable.from(input).flattenRecursive().toArray()
console.log(a) // prints [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]

Returns ChainableIterable for the flattened sequence

map

Generates a sequence of items by calling fn(item) for each item.

Parameters

  • fn Function fn(item) returns the output item
  • iterable Iterable the sequence to map

Examples

const input = [0, 1, 2, 3]
const a = ChainableIterable.from(input).map(x => 2 * x).toArray()
console.log(a) // prints [0, 2, 4, 6]

Returns ChainableIterable for the mapped sequence

mapWith

Map the input sequence to the output sequence with a generator that maps one iterator to another.

This method exists solely so that ChainableIterable supports chaining for an arbitrary generator function.

Parameters

  • generatorFunction Function a function that returns an iterable object, and takes an iterable as a parameter. Typically, this will be a generator function.
  • iterable Iterable the input sequence

Examples

const fn = function * (n, iterable) {
  for (let x of iterable) {
    yield n * x
  }
}
const input = [0, 1, 2, 3, 4]
// If your generator takes additional parameters beyond iterable, you'll need
// to wrap it with another function as shown
const wrapper = (iterable) => fn(3, iterable)
const a = ChainableIterable.from(input).mapWith(wrapper).toArray()
console.log(a) // prints [0, 3, 6, 9, 12]

Returns ChainableIterable for the mapped sequence

nth

Given a sequence of Arrays, output the nth element of each array as a sequence.

Parameters

  • index number the index of the Array to output
  • iterable Iterable the iterable to process

Examples

const input = [[0, 1], [2, 3], [4, 5]]
const a = ChainableIterable.from(input).nth(1).toArray()
console.log(a) // prints [1, 3, 5]

Returns ChainableIterable for the nth elements

pluck

Given a sequence of Objects, output the specified property of each Object as a sequence.

Parameters

  • propertyname string the property to extract from each Object
  • iterable Iterable the input sequence of Objects

Examples

const input = [{'a': 1, 'b': 2}, {'a': 3, 'b': 4}, {'a': 5, 'b': 6}]
const a = ChainableIterable.from(input).pluck('a').toArray()
console.log(a) // prints [1, 3, 5]

Returns ChainableIterable for the plucked items

reject

Reject items when fn(item) returns truthy.

Parameters

  • fn Function fn(item) returns truthy when item should be removed from output sequence
  • iterable Iterable input sequence

Examples

const isEvenNumber = x => x % 2 === 0
const input = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
const a = ChainableIterable.from(input).reject(isEvenNumber).toArray()
console.log(a) // prints [1, 3, 5, 7, 9]

Returns ChainableIterable for the non-rejected items

take

Create an output sequence that is the first n items of the input sequence.

Parameters

  • n number the number of items to take
  • iterable Iterable the input sequence to take items from

Examples

const input = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
const a = ChainableIterable.from(input).take(5).toArray()
console.log(a) // prints [0, 1, 2, 3, 4]

Returns ChainableIterable for the first n items

takeWhile

Output items from the input iterable until fn(item) returns !truthy.

Parameters

  • fn Function fn(item) returns truthy to put item in the output sequence
  • iterable Iterable input sequence

Examples

const input = [0, 1, 2, 3, 4, 5, 6]
const a = ChainableIterable.from(input).takeWhile(x => x != 4).toArray()
console.log(a) // prints [0, 1, 2, 3]

Returns ChainableIterable for the selected items

tap

Pass the input sequence to the output sequence without change, but execute fn(item) for each item in the sequence.

Parameters

  • fn Function fn(item) is called for each item in the sequence
  • iterable Iterable the input sequence

Examples

const input = [1, 2, 3, 4, 5]
const a = ChainableIterable.from(input).tap(console.log).toArray()
// prints [1, 2, 3, 4, 5]

Returns ChainableIterable that is equivalent to the input iterable

Reducers

Methods for reducing a sequence to a single value.

forEach

Executes function fn(item, index) for each item in the iterable sequence provided.

Parameters

  • fn Function a function(item, index), where item is the current item in the sequence, and index is the index of the current item.
  • iterable Iterable the sequence of items to call fn(item, index) with.

Examples

const fn = (item, index) => console.log(`item - ${item}, index - ${index}`)
ChainableIterable.from([1, 2, 3]).forEach(fn) // prints the following...
// item - 1, index - 0
// item - 2, index - 1
// item - 3, index - 2

Returns undefined

reduce

The reduce() method executes a reducer function on each element of the input iterable, resulting in a single output value.

Parameters

  • fn Function fn(accumulator, item) that returns the new accumulator value
  • accumulator any the initial accumulator value
  • iterable Iterable the sequence to execute fn over.

Examples

const add = (a, b) => a + b
const sum = ChainableIterable.from([0, 1, 2, 3, 4]).reduce(add, 0) // sum === 10

Returns any the final accumulator value

toArray

Creates an Array from the items in iterable.

Parameters

  • iterable Iterable the iterable to create the array from

Examples

const a = ChainableIterable.range(5).toArray() // a is [0, 1, 2, 3, 4]

Returns Array the array