class Immutable::Vector(T)

Included Modules

Direct Known Subclasses

Defined in:

immutable/vector/trie.cr
immutable/vector.cr

Constructors

Class Method Summary

Instance Method Summary

Constructor Detail

def self.new(elems : Array(T)) #

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


[View source]
def self.new #

Creates a new empty vector


[View source]

Class Method Detail

def self.[](*elems : T) #

Creates a new vector from the given arguments

vec = Immutable::Vector[1, 2, 3, 4]

[View source]
def self.of(*elems : T) #

Alias for Vector.[]


[View source]

Instance Method Detail

def &(other : Immutable::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]

[View source]
def +(other : Immutable::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"]

[View source]
def -(other : Immutable::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]

[View source]
def <<(elem : T) #

Alias for #push


[View source]
def <=>(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

[View source]
def ==(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

[View source]
def [](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

[View source]
def []?(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

[View source]
def any? #

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


[View source]
def 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

[View source]
def at(i : Int, &block) #

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

[View source]
def 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"

[View source]
def each(&block) #

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

[View source]
def each_index(&block) #

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

[View source]
def empty? #

Returns true if the vector is empty, else false


[View source]
def equals?(other : Vector, &block) #

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

[View source]
def first #

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


[View source]
def first? #

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


[View source]
def hash #

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

See Object#hash.


[View source]
def inspect(io : IO) #

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


[View source]
def last #

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


[View source]
def last? #

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


[View source]
def 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. 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]

[View source]
def pop? : Tuple(T?, Immutable::Vector(T)) #

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


[View source]
def 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"]

[View source]
def 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

[View source]
def size #

Returns the number of elements in the vector


[View source]
def to_json(json : JSON::Builder) #

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


[View source]
def to_s(io : IO) #

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


[View source]
def 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.

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.


[View source]
def 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"]

[View source]
def |(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.

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

[View source]