class Immutable::Vector(T)
- Immutable::Vector(T)
- Reference
- Object
Included Modules
- Comparable(Immutable::Vector(T))
- Enumerable(T)
- Iterable(T)
Direct Known Subclasses
Defined in:
immutable/vector/trie.crimmutable/vector.cr
Constructors
-
.new(elems : Array(T))
Creates a vector filled with the elements from the given array, in the same position.
-
.new
Creates a new empty vector
Class Method Summary
-
.[](*elems : T)
Creates a new vector from the given arguments
-
.of(*elems : T)
Alias for
Vector.[]
Instance Method Summary
-
#&(other : Immutable::Vector(_))
Set intersection: returns a new array containing elements common to the two vectors, excluding any duplicates.
-
#+(other : Immutable::Vector(U)) forall U
Concatenation.
-
#-(other : Immutable::Vector(_))
Difference.
-
#<<(elem : T)
Alias for
#push
-
#<=>(other : Vector)
Combined comparison operator.
-
#==(other : Vector)
Equality.
-
#[](i : Int)
Returns the element at the given index.
-
#[]?(i : Int)
Returns the element at the given index.
-
#any?
Returns true if the vector contains at least one element, else false
-
#at(i : Int)
Returns the element at the given index, if in bounds, otherwise raises
IndexError
-
#at(i : Int, &block)
Returns the element at the given index, if in bounds, otherwise executes the given block and returns its value.
-
#each
Returns an
Iterator
for the elements of this vector. -
#each(&block)
Calls the given block once for each element in this vector, passing that element as a parameter.
-
#each_index(&block)
Calls the given block once for each index in this vector, passing that index as a parameter.
-
#empty?
Returns true if the vector is empty, else false
-
#equals?(other : Vector, &block)
Determines if this vector equals other according to a comparison done by the given block.
-
#first
Returns the first element in the vector, if not empty, else raises
IndexError
-
#first?
Returns the first element in the vector, if not empty, else nil
-
#hash
Returns a hash code based on this vector's size and elements.
-
#inspect(io : IO)
Appends a
String
representation of this object to the given IO object. -
#last
Returns the last element in the vector, if not empty, else raises
IndexError
-
#last?
Returns the last element in the vector, if not empty, else nil
-
#pop : Tuple(T, Immutable::Vector(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?, Immutable::Vector(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.
-
#size
Returns the number of elements in the vector
-
#to_json(json : JSON::Builder)
Appends a JSON string representation of this vector to the given io object
-
#to_s(io : IO)
Appends a String representation of this vector to the given IO object.
-
#transient(&block)
Executes the given block passing a transient version of the vector, then converts the transient vector back to an immutable one and returns it.
-
#uniq
Returns a new vector by removing duplicate values in self.
-
#|(other : Immutable::Vector(U)) forall U
Set union: returns a new vector by joining self with other, excluding any duplicates and preserving the order from the original vector.
Constructor Detail
Creates a vector filled with the elements from the given array, in the same position.
Class Method Detail
Creates a new vector from the given arguments
vec = Immutable::Vector[1, 2, 3, 4]
Instance Method Detail
Set intersection: returns a new array containing elements common to the two vectors, excluding any duplicates. The order is preserved from the original vector.
v1 = Immutable::Vector[1, 1, 3, 5]
v2 = Immutable::Vector[1, 2, 3]
v1 & v2 # => Vector [1, 3]
Concatenation. Returns a new vector built by concatenating self with other. The type of the new vector is the union of the types of self and other.
v1 = Immutable::Vector[1, 2]
v2 = Immutable::Vector[2, 3]
v3 = Immutable::Vector["a"]
v1 + v2 # => Vector [1, 2, 2, 3]
v1 + v3 # => Vector [1, 2, "a"]
Difference. Returns a new vector that is a copy of the original, removing
any items that appear in other
. The order of the original vector is
preserved.
v1 = Immutable::Vector[1, 2, 3]
v2 = Immutable::Vector[2, 1]
v1 - v2 => Vector [3]
Combined comparison operator. Returns 0 if the first vector equals the second, 1 if the first is greater than the second and -1 if the first is smaller than the second.
It compares the elements of both vectors in the same position using the
#<=>
operator, as soon as one of such comparisons returns a non zero
value, that result is the return value of the whole comparison.
If all elements are equal, the comparison is based on the size of the vectors.
Immutable::Vector[8] <=> Immutable::Vector[1, 2, 3] # => 1
Immutable::Vector[2] <=> Immutable::Vector[4, 2, 3] # => -1
Immutable::Vector[1, 2] <=> Immutable::Vector[1, 2] # => 0
Equality. Returns true if it is passed a Vector and #equals?
returns true for both vectors, the caller and the argument.
vec = Immutable::Vector[1, 2, 3]
vec == Immutable::Vector[1, 2, 3] # => true
vec == Immutable::Vector[2, 3] # => false
Returns the element at the given index.
Negative indices can be used to start counting from the end of the vector.
Raises IndexError
if trying to access an element outside the vector's range.
vec = Immutable::Vector['a', 'b', 'c']
vec[0] # => 'a'
vec[2] # => 'c'
vec[-1] # => 'c'
vec[-2] # => 'b'
vec[3] # raises IndexError
vec[-4] # raises IndexError
Returns the element at the given index.
Negative indices can be used to start counting from the end of the vector.
Returns nil
if trying to access an element outside the vector's range.
vec = Immutable::Vector['a', 'b', 'c']
vec[0]? # => 'a'
vec[2]? # => 'c'
vec[-1]? # => 'c'
vec[-2]? # => 'b'
vec[3]? # nil
vec[-4]? # nil
Returns the element at the given index, if in bounds,
otherwise raises IndexError
v = Immutable::Vector[:foo, :bar]
v.at(0) { :baz } # => :foo
v.at(2) { :baz } # => IndexError
Returns the element at the given index, if in bounds, otherwise executes the given block and returns its value.
v = Immutable::Vector[:foo, :bar]
v.at(0) { :baz } # => :foo
v.at(2) { :baz } # => :baz
Returns an Iterator
for the elements of this vector.
v = Immutable::Vector["a", "b", "c"]
iter = v.each
iter.next # => "a"
iter.next # => "b"
Calls the given block once for each element in this vector, passing that element as a parameter.
v = Immutable::Vector["a", "b", "c"]
v.each { |x| print x, " -- " }
produces:
a -- b -- c --
Calls the given block once for each index in this vector, passing that index as a parameter.
v = Immutable::Vector["a", "b", "c"]
v.each_index { |x| print x, " -- " }
produces:
0 -- 1 -- 2 --
Determines if this vector equals other according to a comparison done by the given block.
If this vector's size is the same as other's size, this method yields
elements from this vector and other in tandem: if the block returns true
for all of them, this method returns true
. Otherwise it returns false
.
a = Immutable::Vector[1, 2, 3]
b = Immutable::Vector["a", "ab", "abc"]
a.equals?(b) { |x, y| x == y.size } # => true
a.equals?(b) { |x, y| x == y } # => false
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
Appends a JSON string representation of this vector to the given io object
Executes the given block passing a transient version of the vector, then converts the transient vector back to an immutable one and returns it.
This is useful to perform several updates on a vector in an efficient way: as the transient vector supports the same API of vector, but performs updates in place, avoiding unnecessary object allocations.
vec = Immutable::Vector(Int32).new
v2 = vec.transient do |v|
100.times { |i| v = v.push(i) }
end
v2.size # => 100
Note that, as the transient is mutable, it is not thread-safe.
Returns a new vector by removing duplicate values in self.
v = Immutable::Vector["a", "a", "b", "b", "c"]
v.uniq # => Vector ["a", "b", "c"]
v # => Vector ["a", "a", "b", "b", "c"]
Set union: returns a new vector by joining self with other, excluding any duplicates and preserving the order from the original vector.
v1 = Immutable::Vector["a", "b", "c"]
v2 = Immutable::Vector["c", "d", "a"]
v1 | v2 # => Vector [1, 3] # => Vector ["a", "b", "c", "d"]