Options
All
  • Public
  • Public/Protected
  • All
Menu

@phosphor/algorithm

Index

Type aliases

IterableOrArrayLike

IterableOrArrayLike: IIterable<T> | ArrayLike<T>

A type alias for an iterable or builtin array-like object.

RetroableOrArrayLike

RetroableOrArrayLike: IRetroable<T> | ArrayLike<T>

A type alias for a retroable or builtin array-like object.

Functions

chain

  • Chain together several iterables.

    Type parameters

    • T

    Parameters

    Returns IIterator<T>

    An iterator which yields the values of the iterables in the order in which they are supplied.

    Example

    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]
    

each

  • Invoke a function for each value in an iterable.

    Type parameters

    • T

    Parameters

    • object: IterableOrArrayLike<T>

      The iterable or array-like object of interest.

    • fn: function

      The callback function to invoke for each value.

      Notes

      Iteration can be terminated early by returning false from the callback function.

      Complexity

      Linear.

      Example

      import { each } from '@phosphor/algorithm';
      
      let data = [5, 7, 0, -2, 9];
      
      each(data, value => { console.log(value); });
      
        • (value: T, index: number): boolean | void
        • Parameters

          • value: T
          • index: number

          Returns boolean | void

    Returns void

empty

  • Create an empty iterator.

    Type parameters

    • T

    Returns IIterator<T>

    A new iterator which yields nothing.

    Example

    import { empty, toArray } from '@phosphor/algorithm';
    
    let stream = empty<number>();
    
    toArray(stream);  // []
    

every

  • Test whether all values in an iterable satisfy a predicate.

    Type parameters

    • T

    Parameters

    • object: IterableOrArrayLike<T>

      The iterable or array-like object of interest.

    • fn: function

      The predicate function to invoke for each value.

        • (value: T, index: number): boolean
        • Parameters

          • value: T
          • index: number

          Returns boolean

    Returns boolean

    true if all values pass the test, false otherwise.

    Notes

    Iteration terminates on the first false predicate result.

    Complexity

    Linear.

    Example

    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

  • Filter an iterable for values which pass a test.

    Type parameters

    • T

    Parameters

    • object: IterableOrArrayLike<T>

      The iterable or array-like object of interest.

    • fn: function

      The predicate function to invoke for each value.

        • (value: T, index: number): boolean
        • Parameters

          • value: T
          • index: number

          Returns boolean

    Returns IIterator<T>

    An iterator which yields the values which pass the test.

    Example

    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

  • Find the first value in an iterable which matches a predicate.

    Type parameters

    • T

    Parameters

    • object: IterableOrArrayLike<T>

      The iterable or array-like object to search.

    • fn: function

      The predicate function to apply to the values.

        • (value: T, index: number): boolean
        • Parameters

          • value: T
          • index: number

          Returns boolean

    Returns T | undefined

    The first matching value, or undefined if no matching value is found.

    Complexity

    Linear.

    Example

    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'
    

iter

  • Create an iterator for an iterable object.

    Type parameters

    • T

    Parameters

    Returns IIterator<T>

    A new iterator for the given object.

    Notes

    This function allows iteration algorithms to operate on user-defined iterable types and builtin array-like objects in a uniform fashion.

map

  • Transform the values of an iterable with a mapping function.

    Type parameters

    • T

    • U

    Parameters

    • object: IterableOrArrayLike<T>

      The iterable or array-like object of interest.

    • fn: function

      The mapping function to invoke for each value.

        • (value: T, index: number): U
        • Parameters

          • value: T
          • index: number

          Returns U

    Returns IIterator<U>

    An iterator which yields the transformed values.

    Example

    import { map, toArray } from '@phosphor/algorithm';
    
    let data = [1, 2, 3];
    
    let stream = map(data, value => value * 2);
    
    toArray(stream);  // [2, 4, 6]
    

max

  • Find the maximum value in an iterable.

    Type parameters

    • T

    Parameters

    • object: IterableOrArrayLike<T>

      The iterable or array-like object to search.

    • fn: function

      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.

        • (first: T, second: T): number
        • Parameters

          • first: T
          • second: T

          Returns number

    Returns T | undefined

    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.

    Complexity

    Linear.

    Example

    import { max } from '@phosphor/algorithm';
    
    function numberCmp(a: number, b: number): number {
      return a - b;
    }
    
    max([7, 4, 0, 3, 9, 4], numberCmp);  // 9
    

min

  • Find the minimum value in an iterable.

    Type parameters

    • T

    Parameters

    • object: IterableOrArrayLike<T>

      The iterable or array-like object to search.

    • fn: function

      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.

        • (first: T, second: T): number
        • Parameters

          • first: T
          • second: T

          Returns number

    Returns T | undefined

    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.

    Complexity

    Linear.

    Example

    import { min } from '@phosphor/algorithm';
    
    function numberCmp(a: number, b: number): number {
      return a - b;
    }
    
    min([7, 4, 0, 3, 9, 4], numberCmp);  // 0
    

minmax

  • Find the minimum and maximum values in an iterable.

    Type parameters

    • T

    Parameters

    • object: IterableOrArrayLike<T>

      The iterable or array-like object to search.

    • fn: function

      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.

        • (first: T, second: T): number
        • Parameters

          • first: T
          • second: T

          Returns number

    Returns [T, T] | undefined

    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.

    Complexity

    Linear.

    Example

    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]
    

once

  • Create an iterator which yields a value a single time.

    Type parameters

    • T

    Parameters

    • value: T

      The value to wrap in an iterator.

    Returns IIterator<T>

    A new iterator which yields the value a single time.

    Example

    import { once, toArray } from '@phosphor/algorithm';
    
    let stream = once(7);
    
    toArray(stream);  // [7]
    

range

  • range(start: number, stop?: undefined | number, step?: undefined | number): IIterator<number>
  • Create an iterator of evenly spaced values.

    Parameters

    • start: number

      The starting value for the range, inclusive.

    • Optional stop: undefined | number

      The stopping value for the range, exclusive.

    • Optional step: undefined | number

      The distance between each value.

    Returns IIterator<number>

    An iterator which produces evenly spaced values.

    Notes

    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.

reduce

  • Summarize all values in an iterable using a reducer function.

    Type parameters

    • T

    Parameters

    • object: IterableOrArrayLike<T>

      The iterable or array-like object of interest.

    • fn: function

      The reducer function to invoke for each value.

        • (accumulator: T, value: T, index: number): T
        • Parameters

          • accumulator: T
          • value: T
          • index: number

          Returns T

    Returns T

    The final accumulated value.

    Notes

    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.

    Complexity

    Linear.

    Example

    import { reduce } from '@phosphor/algorithm';
    
    let data = [1, 2, 3, 4, 5];
    
    let sum = reduce(data, (a, value) => a + value);  // 15
    
  • Type parameters

    • T

    • U

    Parameters

    • object: IterableOrArrayLike<T>
    • fn: function
        • (accumulator: U, value: T, index: number): U
        • Parameters

          • accumulator: U
          • value: T
          • index: number

          Returns U

    • initial: U

    Returns U

repeat

  • repeat<T>(value: T, count: number): IIterator<T>
  • Create an iterator which repeats a value a number of times.

    Type parameters

    • T

    Parameters

    • value: T

      The value to repeat.

    • count: number

      The number of times to repeat the value.

    Returns IIterator<T>

    A new iterator which repeats the specified value.

    Example

    import { repeat, toArray } from '@phosphor/algorithm';
    
    let stream = repeat(7, 3);
    
    toArray(stream);  // [7, 7, 7]
    

retro

  • Create an iterator for a retroable object.

    Type parameters

    • T

    Parameters

    Returns IIterator<T>

    An iterator which traverses the object's values in reverse.

    Example

    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]
    

some

  • Test whether any value in an iterable satisfies a predicate.

    Type parameters

    • T

    Parameters

    • object: IterableOrArrayLike<T>

      The iterable or array-like object of interest.

    • fn: function

      The predicate function to invoke for each value.

        • (value: T, index: number): boolean
        • Parameters

          • value: T
          • index: number

          Returns boolean

    Returns boolean

    true if any value passes the test, false otherwise.

    Notes

    Iteration terminates on the first true predicate result.

    Complexity

    Linear.

    Example

    import { some } from '@phosphor/algorithm';
    
    let data = [5, 7, 1];
    
    some(data, value => value === 7);  // true
    some(data, value => value === 3);  // false
    

stride

  • Iterate over an iterable using a stepped increment.

    Type parameters

    • T

    Parameters

    • object: IterableOrArrayLike<T>

      The iterable or array-like object of interest.

    • step: number

      The distance to step on each iteration. A value of less than 1 will behave the same as a value of 1.

    Returns IIterator<T>

    An iterator which traverses the iterable step-wise.

    Example

    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

  • Take a fixed number of items from an iterable.

    Type parameters

    • T

    Parameters

    • object: IterableOrArrayLike<T>

      The iterable or array-like object of interest.

    • count: number

      The number of items to take from the iterable.

    Returns IIterator<T>

    An iterator which yields the specified number of items from the source iterable.

    Notes

    The returned iterator will exhaust early if the source iterable contains an insufficient number of items.

toArray

  • Create an array from an iterable of values.

    Type parameters

    • T

    Parameters

    Returns T[]

    A new array of values from the given object.

    Example

    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];
    

topologicSort

  • Topologically sort an iterable of edges.

    Type parameters

    • T

    Parameters

    • edges: IterableOrArrayLike<[T, T]>

      The iterable or array-like object of edges to sort. An edge is represented as a 2-tuple of [fromNode, toNode].

    Returns T[]

    The topologically sorted array of nodes.

    Notes

    If a cycle is present in the graph, the cycle will be ignored and the return value will be only approximately sorted.

    Example

    ```typescript import { topologicSort } from '@phosphor/algorithm';

    let data = [ ['d', 'e'], ['c', 'd'], ['a', 'b'], ['b', 'c'] ];

    topologicSort(data); // ['a', 'b', 'c', 'd', 'e']

zip

  • Iterate several iterables in lockstep.

    Type parameters

    • T

    Parameters

    Returns IIterator<T[]>

    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.

    Example

    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