Iterable
Index
Accessor
size
Returns the size of iterable
const iterable = Iterable.of(1, 2, 3);size(iterable); // 3
Constructor
create
Iterable constructor
const iterable = Iterable.create(() => ({next() { ... }}))
empty
Returns an iterable that have no value
Array.from(Iterable.empty()) // == []
from
- Iterable.from([1, 2, 3]);// Iterable.of(1, 2, 3)Iterable.from(function* () { yield 1; yield 2; yield 3; })// Iterable.of(1, 2, 3)
generate
Generate an iterable of
lengthusingmapFn(index)on each elementIterable.generate(3, () => 'a');// == Iterable.of('a', 'a', 'a')Iterable.generate(3, (index) => index * 2);// == Iterable.of(0, 2, 4)
of
Create an iterable of given
valuesIterable.of('a', 'b', 'c');// 'a', 'b', 'c'
Predicate
every
Tests whether all elements in the iterable pass the test implemented by the provided function.
const iterable = Iterable.of(1, 2, 3);Iterable.every(iterable, (currentValue) => currentValue >= 1); // trueIterable.every(iterable, (currentValue) => currentValue >= 2); // falseIterable.every(iterable, (currentValue) => currentValue < 0); // false
some
Tests whether any element in the iterable pass the test implemented by the provided function.
const iterable = Iterable.of(1, 2, 3);Iterable.some(iterable, (currentValue) => currentValue >= 1); // trueIterable.some(iterable, (currentValue) => currentValue >= 2); // trueIterable.some(iterable, (currentValue) => currentValue < 0); // false
Type
hasInstance
Returns
trueifanyValueis a valid IterableIterable.hasInstance({});// falseIterable.hasInstance([]);// trueIterable.hasInstance({ [Symbol.iterator]: () => ({ next: () => ({ done: true }) }) });// true
Other
chunks
Splits an iterable into chunks of a specified size
const chunks = Iterable.chunks([1, 2, 3, 4, 5], 2); // == Iterable.of([1, 2], [3, 4], [5])
concat
Concatenates multiple iterables into a single iterable
const iterable = Iterable.of(Iterable.of(1, 2),Iterable.of(3, 4),);Iterable.concat(iterable); // == Iterable.of(1, 2, 3, 4)
drop
Skips the given number of elements at the start of this iterator.
const iterable = Iterable.of(1, 2, 3);Iterable.drop(iterable, 1); // == Iterable.of(2, 3)
filter
Return a new iterable that filters values using
predicateconst iterable = [1, 2, 3];Iterable.filter(iterable,(currentValue, currentIndex) => currentValue > 1,);// == Iterable.of(2, 3)
find
Returns the first element for which the given function returns true, otherwise undefined.
const iterable = Iterable.of({ name: 'amy', id: 1 },{ name: 'bob', id: 2 });find(iterable, (item) => item.name === 'bob') // { name: 'bob', id: 2 }find(iterable, (item) => item.name === 'cat') // undefined
flatMap
Return a new Iterable which applies
mapFnand concatenate the result to the previousconst iterable = Iterable.of(1, 2, 3);Iterable.flatMap(iterable,(currentValue, currentIndex) => Iterable.of(currentValue, currentValue * 2),);// == Iterable.of(1, 2, 2, 4, 3, 6)
map
Return a new Iterable which applies
mapFnto each valuesconst iterable = [1, 2, 3];Iterable.map(iterable,(currentValue, currentIndex) => currentValue * 2,);// == Iterable.of(2, 4, 6)
reduce
Reduce an
initialValueto thereducerfunctionconst iterable = [1, 2, 3];Iterable.reduce(iterable, (total, currentValue, currentIndex) => total + value, 0);// 6
take
Take a specified number of elements from an iterable.
const iterable = Iterable.of(1, 2, 3);Iterable.take(iterable, 2); // == Iterable.of(1, 2)
zip
Combine two iterables into an iterable of couple of their values. The result has the size of the smallest iterable used.
const left = [1, 2, 3];const right = ['a', 'b'];Iterable.zip(left, right);// == Iterable.of([1, 'a'], [2, 'b'])
A collection of functions to manipulate Iterable