Options
All
  • Public
  • Public/Protected
  • All
Menu

Stacks are indexed collections which support very efficient O(1) addition and removal from the front using unshift(v) and shift().

For familiarity, Stack also provides push(v), pop(), and peek(), but be aware that they also operate on the front of the list, unlike List or a JavaScript Array.

Note: reverse() or any inherent reverse traversal (reduceRight, lastIndexOf, etc.) is not efficient with a Stack.

Stack is implemented with a Single-Linked List.

Type parameters

  • T

Callable

  • Create a new immutable Stack containing the values of the provided collection-like.

    The iteration order of the provided collection is preserved in the resulting Stack.

    Note: Stack is a factory function and not a class, and does not use the new keyword during construction.

    Returns Stack<any>

  • Stacks are indexed collections which support very efficient O(1) addition and removal from the front using unshift(v) and shift().

    For familiarity, Stack also provides push(v), pop(), and peek(), but be aware that they also operate on the front of the list, unlike List or a JavaScript Array.

    Note: reverse() or any inherent reverse traversal (reduceRight, lastIndexOf, etc.) is not efficient with a Stack.

    Stack is implemented with a Single-Linked List.

    Type parameters

    • T

    Returns Stack<T>

  • Stacks are indexed collections which support very efficient O(1) addition and removal from the front using unshift(v) and shift().

    For familiarity, Stack also provides push(v), pop(), and peek(), but be aware that they also operate on the front of the list, unlike List or a JavaScript Array.

    Note: reverse() or any inherent reverse traversal (reduceRight, lastIndexOf, etc.) is not efficient with a Stack.

    Stack is implemented with a Single-Linked List.

    Type parameters

    • T

    Parameters

    Returns Stack<T>

Index

Properties

size

size: number

The number of items in this Stack.

Functions

isStack

  • isStack(maybeStack: any): boolean
  • True if the provided value is a Stack

    Parameters

    • maybeStack: any

    Returns boolean

of

  • of<T>(...values: Array<T>): Stack<T>
  • Creates a new Stack containing values.

    Type parameters

    • T

    Parameters

    • Rest ...values: Array<T>

    Returns Stack<T>

Methods

Collection

  • Collection<I>(collection: I): I
  • Creates a Collection.

    The type of Collection created is based on the input.

    • If an Collection, that same Collection.
    • If an Array-like, an Collection.Indexed.
    • If an Object with an Iterator defined, an Collection.Indexed.
    • If an Object, an Collection.Keyed.

    This methods forces the conversion of Objects and Strings to Collections. If you want to ensure that a Collection of one item is returned, use Seq.of.

    Note: An Iterator itself will be treated as an object, becoming a Seq.Keyed, which is usually not what you want. You should turn your Iterator Object into an iterable object by defining a Symbol.iterator (or @@iterator) method which returns this.

    Note: Collection is a conversion function and not a class, and does not use the new keyword during construction.

    Type parameters

    Parameters

    • collection: I

    Returns I

Indexed

  • Creates a new Collection.Indexed.

    Note: Collection.Indexed is a conversion function and not a class, and does not use the new keyword during construction.

    Type parameters

    • T

    Parameters

    Returns Indexed<T>

__@iterator

asImmutable

  • asImmutable(): this
  • see

    Map#asImmutable

    Returns this

asMutable

  • asMutable(): this
  • Note: Not all methods can be used on a mutable collection or within withMutations! Check the documentation for each method to see if it mentions being safe to use in withMutations.

    see

    Map#asMutable

    Returns this

clear

  • Returns a new Stack with 0 size and no values.

    Note: clear can be used in withMutations.

    Returns Stack<T>

concat

  • concat<C>(...valuesOrCollections: Array<Iterable<C> | C>): Stack<T | C>
  • Returns a new Stack with other collections concatenated to this one.

    Type parameters

    • C

    Parameters

    • Rest ...valuesOrCollections: Array<Iterable<C> | C>

    Returns Stack<T | C>

filter

  • filter<F>(predicate: function, context?: any): Set<F>
  • filter(predicate: function, context?: any): this
  • Returns a new Set with only the values for which the predicate function returns true.

    Note: filter() always returns a new instance, even if it results in not filtering out any values.

    Type parameters

    • F: T

    Parameters

    • predicate: function
        • (value: T, index: number, iter: this): boolean
        • Parameters

          • value: T
          • index: number
          • iter: this

          Returns boolean

    • Optional context: any

    Returns Set<F>

  • Parameters

    • predicate: function
        • (value: T, index: number, iter: this): any
        • Parameters

          • value: T
          • index: number
          • iter: this

          Returns any

    • Optional context: any

    Returns this

findIndex

  • findIndex(predicate: function, context?: any): number
  • Returns the first index in the Collection where a value satisfies the provided predicate function. Otherwise -1 is returned.

    Parameters

    • predicate: function
        • (value: T, index: number, iter: this): boolean
        • Parameters

          • value: T
          • index: number
          • iter: this

          Returns boolean

    • Optional context: any

    Returns number

findLastIndex

  • findLastIndex(predicate: function, context?: any): number
  • Returns the last index in the Collection where a value satisfies the provided predicate function. Otherwise -1 is returned.

    Parameters

    • predicate: function
        • (value: T, index: number, iter: this): boolean
        • Parameters

          • value: T
          • index: number
          • iter: this

          Returns boolean

    • Optional context: any

    Returns number

flatMap

  • flatMap<M>(mapper: function, context?: any): Stack<M>
  • Flat-maps the Stack, returning a new Stack.

    Similar to stack.map(...).flatten(true).

    Type parameters

    • M

    Parameters

    • mapper: function
        • (value: T, key: number, iter: this): Iterable<M>
        • Parameters

          • value: T
          • key: number
          • iter: this

          Returns Iterable<M>

    • Optional context: any

    Returns Stack<M>

fromEntrySeq

  • fromEntrySeq(): Keyed<any, any>
  • If this is a collection of [key, value] entry tuples, it will return a Seq.Keyed of those entries.

    Returns Keyed<any, any>

get

  • get<NSV>(index: number, notSetValue: NSV): T | NSV
  • get(index: number): T | undefined
  • Returns the value associated with the provided index, or notSetValue if the index is beyond the bounds of the Collection.

    index may be a negative number, which indexes back from the end of the Collection. s.get(-1) gets the last item in the Collection.

    Type parameters

    • NSV

    Parameters

    • index: number
    • notSetValue: NSV

    Returns T | NSV

  • Parameters

    • index: number

    Returns T | undefined

indexOf

  • indexOf(searchValue: T): number
  • Returns the first index at which a given value can be found in the Collection, or -1 if it is not present.

    Parameters

    • searchValue: T

    Returns number

interleave

  • interleave(...collections: Array<Collection<any, T>>): this
  • Returns a Collection of the same type with the provided collections interleaved into this collection.

    The resulting Collection includes the first item from each, then the second from each, etc.

    const { List } = require('immutable')
    List([ 1, 2, 3 ]).interleave(List([ 'A', 'B', 'C' ]))
    // List [ 1, "A", 2, "B", 3, "C"" ]

    The shortest Collection stops interleave.

    List([ 1, 2, 3 ]).interleave(
      List([ 'A', 'B' ]),
      List([ 'X', 'Y', 'Z' ])
    )
    // List [ 1, "A", "X", 2, "B", "Y"" ]

    Since interleave() re-indexes values, it produces a complete copy, which has O(N) complexity.

    Note: interleave cannot be used in withMutations.

    Parameters

    Returns this

interpose

  • interpose(separator: T): this
  • Returns a Collection of the same type with separator between each item in this Collection.

    Parameters

    • separator: T

    Returns this

lastIndexOf

  • lastIndexOf(searchValue: T): number
  • Returns the last index at which a given value can be found in the Collection, or -1 if it is not present.

    Parameters

    • searchValue: T

    Returns number

map

  • map<M>(mapper: function, context?: any): Stack<M>
  • Returns a new Stack with values passed through a mapper function.

    Stack([ 1, 2 ]).map(x => 10 * x)
    // Stack [ 10, 20 ]

    Note: map() always returns a new instance, even if it produced the same value at every step.

    Type parameters

    • M

    Parameters

    • mapper: function
        • (value: T, key: number, iter: this): M
        • Parameters

          • value: T
          • key: number
          • iter: this

          Returns M

    • Optional context: any

    Returns Stack<M>

peek

  • peek(): T | undefined
  • Alias for Stack.first().

    Returns T | undefined

pop

  • Alias for Stack#shift and is not equivalent to List#pop.

    Returns Stack<T>

push

  • push(...values: Array<T>): Stack<T>
  • Alias for Stack#unshift and is not equivalent to List#push.

    Parameters

    • Rest ...values: Array<T>

    Returns Stack<T>

pushAll

  • Alias for Stack#unshiftAll.

    Parameters

    Returns Stack<T>

shift

  • Returns a new Stack with a size ones less than this Stack, excluding the first item in this Stack, shifting all other values to a lower index.

    Note: this differs from Array#shift because it returns a new Stack rather than the removed value. Use first() or peek() to get the first value in this Stack.

    Note: shift can be used in withMutations.

    Returns Stack<T>

splice

  • splice(index: number, removeNum: number, ...values: Array<T>): this
  • Splice returns a new indexed Collection by replacing a region of this Collection with new values. If values are not provided, it only skips the region to be removed.

    index may be a negative number, which indexes back from the end of the Collection. s.splice(-2) splices after the second to last item.

    const { List } = require('immutable')
    List([ 'a', 'b', 'c', 'd' ]).splice(1, 2, 'q', 'r', 's')
    // List [ "a", "q", "r", "s", "d" ]

    Since splice() re-indexes values, it produces a complete copy, which has O(N) complexity.

    Note: splice cannot be used in withMutations.

    Parameters

    • index: number
    • removeNum: number
    • Rest ...values: Array<T>

    Returns this

toArray

  • toArray(): Array<T>
  • Shallowly converts this collection to an Array.

    Returns Array<T>

toJS

  • toJS(): Array<any>
  • Deeply converts this Indexed collection to equivalent native JavaScript Array.

    Returns Array<any>

toJSON

  • toJSON(): Array<T>
  • Shallowly converts this Indexed collection to equivalent native JavaScript Array.

    Returns Array<T>

toSeq

  • Returns Seq.Indexed.

    override

    Returns Indexed<T>

unshift

  • unshift(...values: Array<T>): Stack<T>
  • Returns a new Stack with the provided values prepended, shifting other values ahead to higher indices.

    This is very efficient for Stack.

    Note: unshift can be used in withMutations.

    Parameters

    • Rest ...values: Array<T>

    Returns Stack<T>

unshiftAll

  • Like Stack#unshift, but accepts a collection rather than varargs.

    Note: unshiftAll can be used in withMutations.

    Parameters

    Returns Stack<T>

wasAltered

  • wasAltered(): boolean
  • see

    Map#wasAltered

    Returns boolean

withMutations

  • withMutations(mutator: function): this
  • Note: Not all methods can be used on a mutable collection or within withMutations! Check the documentation for each method to see if it mentions being safe to use in withMutations.

    see

    Map#withMutations

    Parameters

    • mutator: function
        • (mutable: this): any
        • Parameters

          • mutable: this

          Returns any

    Returns this

zip

  • Returns a Stack "zipped" with the provided collections.

    Like zipWith, but using the default zipper: creating an Array.

    const a = Stack([ 1, 2, 3 ]);
    const b = Stack([ 4, 5, 6 ]);
    const c = a.zip(b); // Stack [ [ 1, 4 ], [ 2, 5 ], [ 3, 6 ] ]

    Type parameters

    • U

    Parameters

    Returns Stack<[T, U]>

  • Type parameters

    • U

    • V

    Parameters

    Returns Stack<[T, U, V]>

  • Parameters

    • Rest ...collections: Array<Collection<any, any>>

    Returns Stack<any>

zipAll

  • Returns a Stack "zipped" with the provided collections.

    Unlike zip, zipAll continues zipping until the longest collection is exhausted. Missing values from shorter collections are filled with undefined.

    const a = Stack([ 1, 2 ]);
    const b = Stack([ 3, 4, 5 ]);
    const c = a.zipAll(b); // Stack [ [ 1, 3 ], [ 2, 4 ], [ undefined, 5 ] ]

    Note: Since zipAll will return a collection as large as the largest input, some results may contain undefined values. TypeScript cannot account for these without cases (as of v2.5).

    Type parameters

    • U

    Parameters

    Returns Stack<[T, U]>

  • Type parameters

    • U

    • V

    Parameters

    Returns Stack<[T, U, V]>

  • Parameters

    • Rest ...collections: Array<Collection<any, any>>

    Returns Stack<any>

zipWith

  • zipWith<U, Z>(zipper: function, otherCollection: Collection<any, U>): Stack<Z>
  • zipWith<U, V, Z>(zipper: function, otherCollection: Collection<any, U>, thirdCollection: Collection<any, V>): Stack<Z>
  • zipWith<Z>(zipper: function, ...collections: Array<Collection<any, any>>): Stack<Z>
  • Returns a Stack "zipped" with the provided collections by using a custom zipper function.

    const a = Stack([ 1, 2, 3 ]);
    const b = Stack([ 4, 5, 6 ]);
    const c = a.zipWith((a, b) => a + b, b);
    // Stack [ 5, 7, 9 ]

    Type parameters

    • U

    • Z

    Parameters

    • zipper: function
        • (value: T, otherValue: U): Z
        • Parameters

          • value: T
          • otherValue: U

          Returns Z

    • otherCollection: Collection<any, U>

    Returns Stack<Z>

  • Type parameters

    • U

    • V

    • Z

    Parameters

    • zipper: function
        • (value: T, otherValue: U, thirdValue: V): Z
        • Parameters

          • value: T
          • otherValue: U
          • thirdValue: V

          Returns Z

    • otherCollection: Collection<any, U>
    • thirdCollection: Collection<any, V>

    Returns Stack<Z>

  • Type parameters

    • Z

    Parameters

    • zipper: function
        • (...any: Array<any>): Z
        • Parameters

          • Rest ...any: Array<any>

          Returns Z

    • Rest ...collections: Array<Collection<any, any>>

    Returns Stack<Z>

Generated using TypeDoc