class Rus3::Vector
A class to represent a vector structure of Scheme.
Public Class Methods
list_to_vector(lst)
click to toggle source
Converts a list into a new Vector
instance.
# File lib/rus3/vector.rb, line 19 def list_to_vector(lst) vec = self.new(lst.size) lst.each_with_index {|e, i| vec.set!(i, e)} vec end
new(k, fill = UNDEF)
click to toggle source
# File lib/rus3/vector.rb, line 38 def initialize(k, fill = UNDEF) @content = Array.new(k, fill) end
vector(*objs)
click to toggle source
Returns a new Vector
instance which elements are the given objs.
# File lib/rus3/vector.rb, line 14 def vector(*objs) list_to_vector(objs) end
vector_set!(vec, k, obj)
click to toggle source
Replaces the k-th element of the given vector with obj.
# File lib/rus3/vector.rb, line 31 def vector_set!(vec, k, obj) raise ExceedUpperLimitError.new(k, vec.length) if k >= vec.length vec.set!(k, obj) end
vector_to_list(vec)
click to toggle source
Converts a Vector
instance into a list.
# File lib/rus3/vector.rb, line 26 def vector_to_list(vec) vec.to_a end
Public Instance Methods
to_a()
click to toggle source
Returns an Array instance which contains the vector contents.
# File lib/rus3/vector.rb, line 47 def to_a @content.dup end
to_s()
click to toggle source
Converts a string represents as a Scheme vector.
# File lib/rus3/vector.rb, line 52 def to_s "#(#{@content.join(' ')})" end