A type alias for a retroable or builtin array-like object.
Chain together several iterables.
The iterable or array-like objects of interest.
An iterator which yields the values of the iterables in the order in which they are supplied.
import { chain, toArray } from '@phosphor/algorithm';
let data1 = [1, 2, 3];
let data2 = [4, 5, 6];
let stream = chain(data1, data2);
toArray(stream); // [1, 2, 3, 4, 5, 6]
Invoke a function for each value in an iterable.
The iterable or array-like object of interest.
The callback function to invoke for each value.
Iteration can be terminated early by returning false
from the
callback function.
Linear.
import { each } from '@phosphor/algorithm';
let data = [5, 7, 0, -2, 9];
each(data, value => { console.log(value); });
Create an empty iterator.
A new iterator which yields nothing.
import { empty, toArray } from '@phosphor/algorithm';
let stream = empty<number>();
toArray(stream); // []
Test whether all values in an iterable satisfy a predicate.
The iterable or array-like object of interest.
The predicate function to invoke for each value.
true
if all values pass the test, false
otherwise.
Iteration terminates on the first false
predicate result.
Linear.
import { every } from '@phosphor/algorithm';
let data = [5, 7, 1];
every(data, value => value % 2 === 0); // false
every(data, value => value % 2 === 1); // true
Filter an iterable for values which pass a test.
The iterable or array-like object of interest.
The predicate function to invoke for each value.
An iterator which yields the values which pass the test.
import { filter, toArray } from '@phosphor/algorithm';
let data = [1, 2, 3, 4, 5, 6];
let stream = filter(data, value => value % 2 === 0);
toArray(stream); // [2, 4, 6]
Find the first value in an iterable which matches a predicate.
The iterable or array-like object to search.
The predicate function to apply to the values.
The first matching value, or undefined
if no matching
value is found.
Linear.
import { find } from '@phosphor/algorithm';
interface IAnimal { species: string, name: string };
function isCat(value: IAnimal): boolean {
return value.species === 'cat';
}
let data: IAnimal[] = [
{ species: 'dog', name: 'spot' },
{ species: 'cat', name: 'fluffy' },
{ species: 'alligator', name: 'pocho' }
];
find(data, isCat).name; // 'fluffy'
Create an iterator for an iterable object.
The iterable or array-like object of interest.
A new iterator for the given object.
This function allows iteration algorithms to operate on user-defined iterable types and builtin array-like objects in a uniform fashion.
Transform the values of an iterable with a mapping function.
The iterable or array-like object of interest.
The mapping function to invoke for each value.
An iterator which yields the transformed values.
import { map, toArray } from '@phosphor/algorithm';
let data = [1, 2, 3];
let stream = map(data, value => value * 2);
toArray(stream); // [2, 4, 6]
Find the maximum value in an iterable.
The iterable or array-like object to search.
The 3-way comparison function to apply to the values.
It should return < 0
if the first value is less than the second.
0
if the values are equivalent, or > 0
if the first value is
greater than the second.
The maximum value in the iterable. If multiple values are
equivalent to the maximum, the left-most value is returned. If
the iterable is empty, this returns undefined
.
Linear.
import { max } from '@phosphor/algorithm';
function numberCmp(a: number, b: number): number {
return a - b;
}
max([7, 4, 0, 3, 9, 4], numberCmp); // 9
Find the minimum value in an iterable.
The iterable or array-like object to search.
The 3-way comparison function to apply to the values.
It should return < 0
if the first value is less than the second.
0
if the values are equivalent, or > 0
if the first value is
greater than the second.
The minimum value in the iterable. If multiple values are
equivalent to the minimum, the left-most value is returned. If
the iterable is empty, this returns undefined
.
Linear.
import { min } from '@phosphor/algorithm';
function numberCmp(a: number, b: number): number {
return a - b;
}
min([7, 4, 0, 3, 9, 4], numberCmp); // 0
Find the minimum and maximum values in an iterable.
The iterable or array-like object to search.
The 3-way comparison function to apply to the values.
It should return < 0
if the first value is less than the second.
0
if the values are equivalent, or > 0
if the first value is
greater than the second.
A 2-tuple of the [min, max]
values in the iterable. If
multiple values are equivalent, the left-most values are returned.
If the iterable is empty, this returns undefined
.
Linear.
import { minmax } from '@phosphor/algorithm';
function numberCmp(a: number, b: number): number {
return a - b;
}
minmax([7, 4, 0, 3, 9, 4], numberCmp); // [0, 9]
Create an iterator which yields a value a single time.
The value to wrap in an iterator.
A new iterator which yields the value a single time.
import { once, toArray } from '@phosphor/algorithm';
let stream = once(7);
toArray(stream); // [7]
Create an iterator of evenly spaced values.
The starting value for the range, inclusive.
The stopping value for the range, exclusive.
The distance between each value.
An iterator which produces evenly spaced values.
In the single argument form of range(stop)
, start
defaults to
0
and step
defaults to 1
.
In the two argument form of range(start, stop)
, step
defaults
to 1
.
Summarize all values in an iterable using a reducer function.
The iterable or array-like object of interest.
The reducer function to invoke for each value.
The final accumulated value.
The reduce
function follows the conventions of Array#reduce
.
If the iterator is empty, an initial value is required. That value will be used as the return value. If no initial value is provided, an error will be thrown.
If the iterator contains a single item and no initial value is provided, the single item is used as the return value.
Otherwise, the reducer is invoked for each element in the iterable. If an initial value is not provided, the first element will be used as the initial accumulated value.
Linear.
import { reduce } from '@phosphor/algorithm';
let data = [1, 2, 3, 4, 5];
let sum = reduce(data, (a, value) => a + value); // 15
Create an iterator which repeats a value a number of times.
The value to repeat.
The number of times to repeat the value.
A new iterator which repeats the specified value.
import { repeat, toArray } from '@phosphor/algorithm';
let stream = repeat(7, 3);
toArray(stream); // [7, 7, 7]
Create an iterator for a retroable object.
The retroable or array-like object of interest.
An iterator which traverses the object's values in reverse.
import { retro, toArray } from '@phosphor/algorithm';
let data = [1, 2, 3, 4, 5, 6];
let stream = retro(data);
toArray(stream); // [6, 5, 4, 3, 2, 1]
Test whether any value in an iterable satisfies a predicate.
The iterable or array-like object of interest.
The predicate function to invoke for each value.
true
if any value passes the test, false
otherwise.
Iteration terminates on the first true
predicate result.
Linear.
import { some } from '@phosphor/algorithm';
let data = [5, 7, 1];
some(data, value => value === 7); // true
some(data, value => value === 3); // false
Iterate over an iterable using a stepped increment.
The iterable or array-like object of interest.
The distance to step on each iteration. A value
of less than 1
will behave the same as a value of 1
.
An iterator which traverses the iterable step-wise.
import { stride, toArray } from '@phosphor/algorithm';
let data = [1, 2, 3, 4, 5, 6];
let stream = stride(data, 2);
toArray(stream); // [1, 3, 5];
Take a fixed number of items from an iterable.
The iterable or array-like object of interest.
The number of items to take from the iterable.
An iterator which yields the specified number of items from the source iterable.
The returned iterator will exhaust early if the source iterable contains an insufficient number of items.
Create an array from an iterable of values.
The iterable or array-like object of interest.
A new array of values from the given object.
import { iter, toArray } from '@phosphor/algorithm';
let data = [1, 2, 3, 4, 5, 6];
let stream = iter(data);
toArray(stream); // [1, 2, 3, 4, 5, 6];
Topologically sort an iterable of edges.
The iterable or array-like object of edges to sort.
An edge is represented as a 2-tuple of [fromNode, toNode]
.
The topologically sorted array of nodes.
If a cycle is present in the graph, the cycle will be ignored and the return value will be only approximately sorted.
`
typescript
import { topologicSort } from '@phosphor/algorithm';
let data = [ ['d', 'e'], ['c', 'd'], ['a', 'b'], ['b', 'c'] ];
topologicSort(data); // ['a', 'b', 'c', 'd', 'e']
Iterate several iterables in lockstep.
The iterable or array-like objects of interest.
An iterator which yields successive tuples of values where each value is taken in turn from the provided iterables. It will be as long as the shortest provided iterable.
import { zip, toArray } from '@phosphor/algorithm';
let data1 = [1, 2, 3];
let data2 = [4, 5, 6];
let stream = zip(data1, data2);
toArray(stream); // [[1, 4], [2, 5], [3, 6]]
Generated using TypeDoc
A type alias for an iterable or builtin array-like object.