Immutable::Vector::Transient(T)
Inherits Immutable::Vector < Comparable < Iterable < Enumerable < Reference < Object
Constructors
new(trie : Trie(T), tail : Array(T))
Sourcenew(elems : Array(T))
Sourcenew
SourceInstance methods
persist!
Sourcepop
Return a tuple of two things: the last element of the vector and a copy of
the vector with the last element removed. Raises IndexError if the
vector is empty.
v = Immutable::Vector[1, 2, 3, 4]
last, v2 = v.pop
last # => 4
v2 # => Vector [1, 2, 3]
push(elem : T)
Returns a new vector with the given value appended to the end, given that the type of the value is T (which might be a type or a union of types).
v = Immutable::Vector["a", "b"]
v.push("c") # => Vector ["a", "b", "c"]
v.push(1) # => Errors, because the vector only accepts String
# The original vector remains unchanged:
v # => Vector ["a", "b"]
set(i : Int, value : T)
Returns a modified copy of the vector with the element at the given index set to the given value.
Negative indices can be used to start counting from the end of the vector.
Raises IndexError if trying to set an element outside the vector's range.
vec = Immutable::Vector[1, 2, 3]
vec.set(0, 5) # Vector [5, 2, 3]
vec # Vector [1, 2, 3]
vec.set(3, 5) # => IndexError