Class: Nydp::Pair
Constant Summary
collapse
- NIL =
Nydp::NIL
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Methods included from Helper
cons, list, literal?, pair?, sig, sym, sym?
Methods included from Converter
#n2r, #r2n
Constructor Details
#initialize(car, cdr) ⇒ Pair
8
9
10
|
# File 'lib/nydp/pair.rb', line 8
def initialize car, cdr
@car, @cdr = car, cdr
end
|
Instance Attribute Details
Returns the value of attribute car.
6
7
8
|
# File 'lib/nydp/pair.rb', line 6
def car
@car
end
|
Returns the value of attribute cdr.
6
7
8
|
# File 'lib/nydp/pair.rb', line 6
def cdr
@cdr
end
|
Class Method Details
.from_list(list, last = Nydp::NIL, n = 0) ⇒ Object
66
67
68
69
70
71
72
|
# File 'lib/nydp/pair.rb', line 66
def self.from_list list, last=Nydp::NIL, n=0
if n >= list.size
last
else
Nydp::Pair.new list[n], from_list(list, last, n+1)
end
end
|
.parse_list(list) ⇒ Object
58
59
60
61
62
63
64
|
# File 'lib/nydp/pair.rb', line 58
def self.parse_list list
if sym? list.slice(-2), "."
from_list(list[0...-2], list.slice(-1))
else
from_list list
end
end
|
Instance Method Details
26
|
# File 'lib/nydp/pair.rb', line 26
def & other ; self.class.from_list((Set.new(self) & other).to_a) ; end
|
23
|
# File 'lib/nydp/pair.rb', line 23
def + other ; copy.append other ; end
|
28
|
# File 'lib/nydp/pair.rb', line 28
def - other ; self.class.from_list((Set.new(self) - other).to_a) ; end
|
#==(other) ⇒ Object
74
75
76
|
# File 'lib/nydp/pair.rb', line 74
def == other
(NIL != other) && (other.respond_to? :car) && (self.car == other.car) && (self.cdr == other.cdr)
end
|
#append(thing) ⇒ Object
147
148
149
150
151
152
153
154
155
156
|
# File 'lib/nydp/pair.rb', line 147
def append thing
if Nydp::NIL.is? self.cdr
self.cdr = thing
elsif pair? self.cdr
self.cdr.append thing
else
raise "can't append #{thing} to list #{self} : cdr is #{self.cdr.inspect}"
end
self
end
|
13
|
# File 'lib/nydp/pair.rb', line 13
def caar ; car.car ; end
|
14
|
# File 'lib/nydp/pair.rb', line 14
def cadr ; cdr.car ; end
|
15
|
# File 'lib/nydp/pair.rb', line 15
def cdar ; car.cdr ; end
|
16
|
# File 'lib/nydp/pair.rb', line 16
def cddr ; cdr.cdr ; end
|
22
|
# File 'lib/nydp/pair.rb', line 22
def copy ; cons(car, cdr.copy) ; end
|
#each {|car| ... } ⇒ Object
78
79
80
81
|
# File 'lib/nydp/pair.rb', line 78
def each &block
yield car
cdr.each(&block) unless Nydp::NIL.is?(cdr)
end
|
#eql?(other) ⇒ Boolean
21
|
# File 'lib/nydp/pair.rb', line 21
def eql? other ; self == other ; end
|
def hash ; @_hash ||= (car.hash + cdr.hash) ; end
20
|
# File 'lib/nydp/pair.rb', line 20
def hash ; (car.hash + cdr.hash) ; end
|
#index_of(x) ⇒ Object
31
32
33
34
35
36
37
38
39
|
# File 'lib/nydp/pair.rb', line 31
def index_of x
if x == car
0
elsif pair?(cdr)
1 + cdr.index_of(x)
else
nil
end
end
|
25
|
# File 'lib/nydp/pair.rb', line 25
def inspect ; "(#{inspect_rest})" ; end
|
#inspect_rest ⇒ Object
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
|
# File 'lib/nydp/pair.rb', line 131
def inspect_rest
res = [car.inspect]
it = cdr
while it && it != Nydp::NIL
if it.is_a?(self.class)
res << it.car.inspect
it = it.cdr
else
res << "."
res << it.inspect
it = nil
end
end
res.compact.join " "
end
|
#nydp_type ⇒ Object
12
|
# File 'lib/nydp/pair.rb', line 12
def nydp_type ; :pair ; end
|
#proper? ⇒ Boolean
29
|
# File 'lib/nydp/pair.rb', line 29
def proper? ; Nydp::NIL.is?(cdr) || (cdr.is_a?(Nydp::Pair) && cdr.proper?) ; end
|
24
|
# File 'lib/nydp/pair.rb', line 24
def size ; 1 + (cdr.is_a?(Nydp::Pair) ? cdr.size : 0) ; end
|
#to_a(list = []) ⇒ Object
returns Array of elements as they are
53
54
55
56
|
# File 'lib/nydp/pair.rb', line 53
def to_a list=[]
list << car
cdr.is_a?(Nydp::Pair) ? cdr.to_a(list) : list
end
|
#to_ruby(list = [], pair = self) ⇒ Object
returns Array of elements after calling #n2r on each element
42
43
44
45
46
47
48
49
50
|
# File 'lib/nydp/pair.rb', line 42
def to_ruby list=[], pair=self
list << n2r(pair.car)
while(pair.cdr.is_a?(Nydp::Pair))
pair = pair.cdr
list << n2r(pair.car)
end
list
end
|
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
|
# File 'lib/nydp/pair.rb', line 83
def to_s
if car.is_a?(Nydp::Symbol) && car.is?(:quote)
if Nydp::NIL.is? cdr.cdr
"'#{cdr.car.to_s}"
else
"'#{cdr.to_s}"
end
elsif car.is_a?(Nydp::Symbol) && car.is?(:"brace-list")
if Nydp::NIL.is? cdr
"{}"
else
"{ #{cdr.to_s_rest} }"
end
elsif car.is_a?(Nydp::Symbol) && car.is?(:quasiquote)
if Nydp::NIL.is? cdr.cdr
"`#{cdr.car.to_s}"
else
"`#{cdr.to_s}"
end
elsif car.is_a?(Nydp::Symbol) && car.is?(:unquote)
if Nydp::NIL.is? cdr.cdr
",#{cdr.car.to_s}"
else
",#{cdr.to_s}"
end
elsif car.is_a?(Nydp::Symbol) && car.is?(:"unquote-splicing")
if Nydp::NIL.is? cdr.cdr
",@#{cdr.car.to_s}"
else
",@#{cdr.to_s}"
end
else
"(#{to_s_rest})"
end
end
|
#to_s_rest ⇒ Object
119
120
121
122
123
124
125
126
127
128
129
|
# File 'lib/nydp/pair.rb', line 119
def to_s_rest
cdr_s = if cdr.is_a?(self.class)
cdr.to_s_rest
elsif Nydp::NIL.is? cdr
nil
else
". #{cdr.to_s}"
end
[car.to_s, cdr_s].compact.join " "
end
|
27
|
# File 'lib/nydp/pair.rb', line 27
def | other ; self.class.from_list((Set.new(self) | other).to_a) ; end
|