class

Immutable::Vector(T)

Inherits Comparable < Iterable < Enumerable < Reference < Object

Constructors

new(elems : Array(T))

Creates a vector filled with the elements from the given array, in the same position.

Source
new

Creates a new empty vector

Source

Class methods

[](*elems : T)

Creates a new vector from the given arguments

vec = Immutable::Vector[1, 2, 3, 4]
Source
of(*elems : T)

Alias for Vector.[]

Source

Instance methods

&(other : Vector(_))

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]
Source
+(other : Vector(U)) forall U

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"]
Source
-(other : Vector(_))

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]
Source
<<(elem : T)

Alias for push

Source
<=>(other : Vector)

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
Source
==(other : Vector)

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
Source
[](i : Int)

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
Source
[]?(i : Int)

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
Source
|(other : 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.

v1 = Immutable::Vector["a", "b", "c"]
v2 = Immutable::Vector["c", "d", "a"]
v1 | v2 # => Vector [1, 3] # => Vector ["a", "b", "c", "d"]
Source
any?

Returns true if the vector contains at least one element, else false

Source
at(i : Int)

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
Source
at(i : Int, &)

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
Source
each

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 --
Source
each

Returns an Iterator for the elements of this vector.

v = Immutable::Vector["a", "b", "c"]
iter = v.each
iter.next # => "a"
iter.next # => "b"
Source
each_index

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 --
Source
empty?

Returns true if the vector is empty, else false

Source
equals?(other : Vector, &)

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
Source
first

Returns the first element in the vector, if not empty, else raises IndexError

Source
first?

Returns the first element in the vector, if not empty, else nil

Source
hash

Returns a hash code based on this vector's size and elements.

See Object#hash.

Source
inspect(io : IO)

Appends a String representation of this object to the given IO object.

Source
last

Returns the last element in the vector, if not empty, else raises IndexError

Source
last?

Returns the last element in the vector, if not empty, else nil

Source
pop

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]
Source
pop?

Like pop, but returns a tuple of nil and empty vector if called on an empty vector

Source
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"]
Source
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
Source
size

Returns the number of elements in the vector

Source
to_json(json : JSON::Builder)

Appends a JSON string representation of this vector to the given io object

Source
to_s(io : IO)

Appends a String representation of this vector to the given IO object.

Source
transient

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.

Source
uniq

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"]
Source

Nested types