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 can be called as a function to convert an iterable to a chainable iterable.
iterable
Iterable - iterable to make chainable
import { chainable } from 'iterablefu'
const a = chainable([1, 2, 3]).map(x => x * x).toArray()
console.log(a) // prints [1, 4, 9]
Returns ChainableIterable
Static methods for creating ChainableIterables.
Concatenates a list of iterables into a single iterable.
iterables
...Iterable to be concatenated
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
Creates a sequence from the input sequence. This function exists solely so that ChainableIterable has a static constructor method.
iterable
inputIterable
Iterable the iterable
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
Creates a sequence of numbers similar to the Python range. See the example.
args
...integer per the example
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
Generates a sequence of things, n times
n
number the number of times to repeat thingthing
any the repeated thing
const generator = chainable.repeat(5, 'a')
console.log([...generator]) // prints ['a', 'a', 'a', 'a', 'a']
Returns ChainableIterable that will repeat thing, n times
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...
n
number number of times to repeat iterablerepeatableIterable
Iterable the input iterable to repeat, see notes above and 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
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.
iterables
...Iterable the iterables to be zipped
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
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.
iterables
...Iterable the iterables to be zipped
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