Class: Lisp::Vector

Inherits:
Atom show all
Defined in:
lib/rubylisp/vector.rb

Instance Attribute Summary

Attributes inherited from Atom

#value

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Atom

#all?, #apply_to, #car, #cdr, #character?, #class?, #copy, #doc, #environment?, #eof_object?, #eq?, #eqv?, #evaluate, #false?, #frame?, #function?, #lisp_object?, #list?, #macro?, #negative?, #number?, #object?, #pair?, #port?, #positive?, #primitive?, #print_string, #quoted, #set!, #set_location, #special?, #string?, #symbol?, #true?, #zero?

Constructor Details

#initialize(a = []) ⇒ Vector

Returns a new instance of Vector.



10
11
12
13
# File 'lib/rubylisp/vector.rb', line 10

def initialize(a = [])
  @value = a
  self
end

Class Method Details

.with_array(a) ⇒ Object



5
6
7
# File 'lib/rubylisp/vector.rb', line 5

def self.with_array(a)
  self.new(a)
end

Instance Method Details

#add(e) ⇒ Object



41
42
43
# File 'lib/rubylisp/vector.rb', line 41

def add(e)
  @value << e
end

#at(n) ⇒ Object Also known as: nth



56
57
58
# File 'lib/rubylisp/vector.rb', line 56

def at(n)
  @value[n]
end

#at_put(n, d) ⇒ Object



68
69
70
# File 'lib/rubylisp/vector.rb', line 68

def at_put(n, d)
  @value[n] = d
end

#each(&block) ⇒ Object



87
88
89
# File 'lib/rubylisp/vector.rb', line 87

def each &block
  @value.each &block
end

#empty?Boolean

Returns:



31
32
33
# File 'lib/rubylisp/vector.rb', line 31

def empty?
  @value.empty?
end

#equal?(other) ⇒ Boolean

Returns:



78
79
80
81
82
83
84
85
# File 'lib/rubylisp/vector.rb', line 78

def equal?(other)
  return false unless other.vector?
  return false unless @value.size == other.value.size
  (0..@value.size).each do |i|
    return false unless other.value[i].equal?(value[i])
  end
  true
end

#lengthObject



36
37
38
# File 'lib/rubylisp/vector.rb', line 36

def length
  @value.size
end

#nth_tail(n) ⇒ Object



62
63
64
65
# File 'lib/rubylisp/vector.rb', line 62

def nth_tail(n)
  return Lisp::Vector.new if n > @value.size
  Lisp::Vector.new(@value[n..-1])
end

#set_nth!(n, d) ⇒ Object



73
74
75
# File 'lib/rubylisp/vector.rb', line 73

def set_nth!(n, d)
  at_put(n, d)
end

#to_aObject



46
47
48
# File 'lib/rubylisp/vector.rb', line 46

def to_a
  @value
end

#to_sObject



51
52
53
# File 'lib/rubylisp/vector.rb', line 51

def to_s
  "#(#{@value.join(' ')})"
end

#typeObject



20
21
22
# File 'lib/rubylisp/vector.rb', line 20

def type
  :vector
end

#update!(a) ⇒ Object



16
17
18
# File 'lib/rubylisp/vector.rb', line 16

def update!(a)
  @value = a
end

#vector?Boolean

Returns:



25
26
27
# File 'lib/rubylisp/vector.rb', line 25

def vector?
  true
end