Returns an iterable with all items of iterable, except for the first limit ones.
assert.deepEqual(
toArray(Iterable.drop(1, ['a', 'b', 'c'])),
['b', 'c']
);
Iterates over each item of iterable and returns false as soon as
pred(item) returns a falsy value. It short-circuits in that case and
doesn’t visit the remaining items of iterable. That means, we can use this
function with iterables of infinite length.
This function returns true if pred() never returns a falsy value
and iterable is of finite length.
function* naturalNumbers() {
for(let i=0;; i++) {
yield i;
}
}
assert.equal(
Iterable.every((item) => item > 0, [5, -3, 12]),
false
);
assert.equal(
Iterable.every((item) => item < 0, [5, -3, 12]),
false
);
assert.equal(
Iterable.every((item) => item >= -3, [5, -3, 12]),
true
);
assert.equal(
Iterable.every((item) => item <= 10, naturalNumbers()),
false
);
This function returns an iterable that contains only those items of iterable
for which filterFn returns a truthy value.
assert.deepEqual(
toArray(Iterable.filter(x => x < 0, [-1, 3, -4, 8])),
[-1, -4]
);
Iterates over each item of iterable and returns item as soon as
pred(item) returns a truthy value. It short-circuits in that case and
doesn’t visit the remaining items of iterable. That means, we can use this
function with iterables of infinite length.
This function returns undefined if pred() never returns a truthy value
and iterable is of finite length.
function* naturalNumbers() {
for(let i=0;; i++) {
yield i;
}
}
assert.equal(
Iterable.find((item) => item > 0, [5, -3, 12, -8]),
5
);
assert.equal(
Iterable.find((item) => item < 0, [5, -3, 12, -8]),
-3
);
assert.equal(
Iterable.find((item) => item > 20, naturalNumbers()),
21
);
Each item of iterable is converted to zero or more items in the returned
iterable, depending on whether mapperFn returns a single value or an Array
(and on how long that Array is). This enables us to:
[] and map by returning [result].assert.deepEqual(
toArray(Iterable.flatMap(x => x, ['a', 'b', 'c'])),
['a', 'b', 'c']
);
assert.deepEqual(
toArray(Iterable.flatMap(x => [], ['a', 'b', 'c'])),
[]
);
assert.deepEqual(
toArray(Iterable.flatMap(x => [x], ['a', 'b', 'c'])),
['a', 'b', 'c']
);
assert.deepEqual(
toArray(Iterable.flatMap(x => [x, x], ['a', 'b', 'c'])),
['a', 'a', 'b', 'b', 'c', 'c']
);
Retrieves each item of iterable and invokes fn() with it.
const result = [];
Iterable.forEach(x => result.push(x + x), ['a', 'b', 'c'])
assert.deepEqual(
result,
['aa', 'bb', 'cc']
);
This function returns an iterable where each item was produced by applying
mapperFn to an item of iterable.
assert.deepEqual(
toArray(Iterable.map(x => x + x, ['a', 'b', 'c'])),
['aa', 'bb', 'cc']
);
Feeds all items of iterable to reducer() which folds them into acc.
The last acc returned by reducer() is the result of this function.
assert.deepEqual(
Iterable.reduce((acc, item) => acc + item, ['a', 'b', 'c']),
'abc'
);
assert.deepEqual(
Iterable.reduce((acc, item) => acc + item, 'x', ['a', 'b', 'c']),
'xabc'
);
The first value of acc. If it is missing, the first item of iterable is used, instead.
Iterates over each item of iterable and returns true as soon as
pred(item) returns a truthy value. It short-circuits in that case and
doesn’t visit the remaining items of iterable. That means, we can use this
function with iterables of infinite length.
This function returns false if pred() never returns a truthy value
and iterable is of finite length.
function* naturalNumbers() {
for(let i=0;; i++) {
yield i;
}
}
assert.equal(
await AsyncIterable.some((item) => item > 0, fi([5, -3, 12])),
true
);
assert.equal(
await AsyncIterable.some((item) => item < 0, fi([5, -3, 12])),
true
);
assert.equal(
await AsyncIterable.some((item) => item < -3, fi([5, -3, 12])),
false
);
assert.equal(
await AsyncIterable.some((item) => item > 10, fi(naturalNumbers())),
true
);
Returns an iterable with the first limit items of iterable.
function* naturalNumbers() {
for(let i=0;; i++) {
yield i;
}
}
assert.deepEqual(
toArray(Iterable.take(3, naturalNumbers())),
[0, 1, 2]
);
assert.deepEqual(
toArray(Iterable.take(2, ['a', 'b', 'c'])),
['a', 'b']
);
Depending on its argument, this function converts:
assert.deepEqual(
toArray(Iterable.zip({first: ['a', 'b'], second: [0, 1, 2] })),
[ {first: 'a', second: 0}, {first: 'b', second: 1} ]
);
assert.deepEqual(
toArray(Iterable.zip([ ['a', 'b'], [0, 1, 2] ])),
[ ['a', 0], ['b', 1] ]
);
Generated using TypeDoc
Each item
xofiterableis converted to an item[index, x]in the returned iterable.While Array methods such as
.map()and.filter()always provide indices, the functions in this module don’t. Therefore, this function is useful whenever we need indices – e.g., to determine which item is first in an iterable.