This prototype object allows easy access to various helpful built-in methods for manipulating an ImmutableVector.
Returns an ImmutableVector
using an optional iterable in a similar way to the Array
constructor except it cannot be called with new
. It is not constructable.
Note: The Array
constructor has a Array(length)
but we don't need an equivalent here because an ImmutableVector
cannot have holes and prespecifying a length has no meaning. Therefore the call ImmutableVector(123)
returns a single element vector with the first value being 123
.
Accessing index properties (e.g. vector[index]
) and vector.length
behaves in a similar way as for strings that are also value types with indexable properties. Except the element value is returned for the index, rather than a new string.
Creates a new ImmutableVector
with the same values except it copies the sequence of elements within the array-like object or vector to the position starting at target. Note: It returns a new ImmutableVector
, it doesn't mutate the original vector.
Fills all the elements of an array-like object or vector from a start index to an end index with a value. Note: It returns a new ImmutableVector
, it doesn't mutate the original vector.
Returns a new ImmutableVector
with a excluding the last index in this
. Note: It does not mutate the original vector. This differs from its Array
equivalent because it returns the new ImmutableVector
rather than the removed value. Use vector[vector.length - 1 ]
to get the last value.
Returns an ImmutableVector
with all the elements of the original array-like object and with each item
added to the end of the vector. Note: It does not mutate the original vector. Unlike its Array
equivalent, it returns a new ImmutableVector
. It does not return the new length
. That use case have to be a separate operation to get vector.length
.
Returns a ImmutableVector
with all the elements of the original array-like object or vector, in the reverse order of the original vector.
Returns an ImmutableVector
with a length ones less than this
, excluding the first index, shifting all other values to a lower index. Note: this differs from its Array
equivalent because it returns a new ImmutableVector
rather than the removed value. Use vector[0]
to get the first value.
Changes the content of an array by removing existing elements and/or adding new elements. Note: It does not mutate the original array-like object. It returns the new ImmutableVector
. Note: Unlike its Array
equivalent, it does not return the removed items. That use case can be solved by calling slice
on the original vector, either before or after.
Returns a ImmutableVector
with all the elements of the original array-like object or vector, sorted according to the compareFn
. Note: This is .
Adds one or more elements to the beginning of an array. Note: It does not mutate the original vector. Unlike its Array
equivalent, it returns a new ImmutableVector
. It does not return the new length
. That use case have to be a separate operation to get vector.length
.
The rest of these are just what you would expect and has similar semantics as the mutable Array
, except they return an ImmutableVector
for operations like filter
, map
etc. It is plausible that these methods are not needed and that they could just go on iterables as part of a more generic combinators API.
There is no need for an equivalence to Array.of ( ...items )
and Array.from
because ImmutableVector
cannot be subclassed and it is unobservable if a copy or the same one is returned.