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

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

Constructor Details

#initialize(a = []) ⇒ Vector

Returns a new instance of Vector.



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

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

Class Method Details

.make_vector_impl(args, env) ⇒ Object



10
11
12
13
14
15
16
17
18
19
# File 'lib/rubylisp/vector.rb', line 10

def self.make_vector_impl(args, env)
  c = args
  a = []
  while !c.nil?
    a << c.car.evaluate(env)
    c = c.cdr
  end

  Lisp::Vector.new(a)
end

.registerObject



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

def self.register
  Primitive.register("make-vector")   {|args, env| Lisp::Vector::make_vector_impl(args, env) }
  Primitive.register("vector")   {|args, env| Lisp::Vector::vector_impl(args, env) }
end

.vector_impl(args, env) ⇒ Object



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

def self.vector_impl(args, env)
  raise "vector requires a single list argument." unless args.length == 1
  
  c = args.car.evaluate(env)
  Lisp::Vector.new(c.to_a)
end

.with_array(a) ⇒ Object



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

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

Instance Method Details

#add(e) ⇒ Object



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

def add(e)
  @value << e
end

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



77
78
79
# File 'lib/rubylisp/vector.rb', line 77

def at(n)
  @value[n - 1]
end

#at_put(n, d) ⇒ Object



89
90
91
# File 'lib/rubylisp/vector.rb', line 89

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

#each(&block) ⇒ Object



108
109
110
# File 'lib/rubylisp/vector.rb', line 108

def each &block
  @value.each &block
end

#empty?Boolean

Returns:



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

def empty?
  @value.empty?
end

#eq?(other) ⇒ Boolean

Returns:



99
100
101
102
103
104
105
106
# File 'lib/rubylisp/vector.rb', line 99

def eq?(other)
  return false unless other.vector?
  return false unless @value.size == other.value.size
  (0..@value.size).each do |i|
    return false unless Lisp::Equivalence.equal_check(other.value[i], value[i]).value
  end
  true
end

#lengthObject



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

def length
  @value.size
end

#nth_tail(n) ⇒ Object



83
84
85
86
# File 'lib/rubylisp/vector.rb', line 83

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

#set_nth!(n, d) ⇒ Object



94
95
96
# File 'lib/rubylisp/vector.rb', line 94

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

#to_aObject



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

def to_a
  @value
end

#to_sObject



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

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

#typeObject



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

def type
  :vector
end

#vector?Boolean

Returns:



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

def vector?
  true
end