class Immutable::Vector::Transient(T)
- Immutable::Vector::Transient(T)
- Immutable::Vector(T)
- Reference
- Object
Defined in:
immutable/vector.crConstructors
Instance Method Summary
- #persist!
-
#pop : Tuple(T, Transient(T))
Return a tuple of two things: the last element of the vector and a copy of the vector with the last element removed.
-
#pop? : Tuple(T | Nil, Transient(T))
Like
#pop
, but returns a tuple of nil and empty vector if called on an empty vector -
#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).
-
#set(i : Int, value : T)
Returns a modified copy of the vector with the element at the given index set to the given value.
Instance methods inherited from class Immutable::Vector(T)
&(other : Vector(_))
&,
+(other : Vector(U)) forall U
+,
-(other : Vector(_))
-,
<<(elem : T)
<<,
<=>(other : Vector)
<=>,
==(other : Vector)
==,
[](i : Int)
[],
[]?(i : Int)
[]?,
|(other : Vector(U)) forall U
|,
any?
any?,
at(i : Int)at(i : Int, &) at, each(&)
each each, each_index(&) each_index, empty? empty?, equals?(other : Vector, &) equals?, first first, first? first?, hash hash, inspect(io : IO) inspect, last last, last? last?, pop : Tuple(T, Vector(T)) pop, pop? : Tuple(T | Nil, Vector(T)) pop?, push(elem : T) push, set(i : Int, value : T) set, size size, to_json(json : JSON::Builder) to_json, to_s(io : IO) to_s, transient(&) transient, uniq uniq
Constructor methods inherited from class Immutable::Vector(T)
new(elems : Array(T))new new
Class methods inherited from class Immutable::Vector(T)
[](*elems : T)
[],
of(*elems : T)
of
Constructor Detail
Instance Method Detail
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]
Like #pop
, but returns a tuple of nil and empty vector if called on an
empty vector
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"]
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