Generator methods to create an iterable sequence from something else.
The IterableFu
package exports the generators as an Object.
import { generators } from 'iterablefu'
console.log(...generators.range(5)) // prints 0 1 2 3 4
Concatenates a list of iterables into a single iterable.
iterables
...Iterable to be concatenated
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
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 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
Creates a sequence of numbers similar to the Python range. See the example.
args
...integer per the example
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
Generates a sequence of things, n times
n
number the number of times to repeat thingthing
any the repeated thing
const generator = repeat(5, 'a')
console.log([...generator]) // prints ['a', 'a', 'a', 'a', 'a']
Returns Generator 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 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
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 = zip(a, b)
console.log([...generator]) // prints [[0, 'a'], [1, 'b'], [2, 'c']]
Returns Generator 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 = zipAll(a, b)
console.log([...generator]) // prints [[0, 'a'], [1, 'b'], [2, 'c'], [undefined, 'd']]
Returns Generator for the zipped arrays