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.
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.
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.
The number of items in this Stack.
True if the provided value is a Stack
Creates a new Stack containing values
.
Creates a Collection.
The type of Collection created is based on the input.
Collection
, that same Collection
.Collection.Indexed
.Collection.Indexed
.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.
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
.
Returns a new Stack with 0 size and no values.
Note: clear
can be used in withMutations
.
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.
Returns the first index in the Collection where a value satisfies the provided predicate function. Otherwise -1 is returned.
Returns the last index in the Collection where a value satisfies the provided predicate function. Otherwise -1 is returned.
If this is a collection of [key, value] entry tuples, it will return a Seq.Keyed of those entries.
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.
Returns the first index at which a given value can be found in the Collection, or -1 if it is not present.
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
.
Returns a Collection of the same type with separator
between each item
in this Collection.
Returns the last index at which a given value can be found in the Collection, or -1 if it is not present.
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.
Alias for Stack.first()
.
Alias for Stack#shift
and is not equivalent to List#pop
.
Alias for Stack#unshift
and is not equivalent to List#push
.
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
.
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
.
Shallowly converts this collection to an Array.
Deeply converts this Indexed collection to equivalent native JavaScript Array.
Shallowly converts this Indexed collection to equivalent native JavaScript Array.
Returns Seq.Indexed.
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
.
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
.
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 ] ]
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).
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 ]
Generated using TypeDoc
Stacks are indexed collections which support very efficient O(1) addition and removal from the front using
unshift(v)
andshift()
.For familiarity, Stack also provides
push(v)
,pop()
, andpeek()
, 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.