Class: Nydp::Pair

Inherits:
Object show all
Extended by:
Helper
Includes:
Enumerable, Helper
Defined in:
lib/nydp/pair.rb

Constant Summary collapse

NIL =
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, #rubify

Constructor Details

#initialize(car, cdr) ⇒ Pair

Returns a new instance of Pair.



8
9
10
# File 'lib/nydp/pair.rb', line 8

def initialize car, cdr
  @car, @cdr = car, cdr
end

Instance Attribute Details

#carObject

Returns the value of attribute car.



6
7
8
# File 'lib/nydp/pair.rb', line 6

def car
  @car
end

#cdrObject

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 = nil, n = list.size-1) ⇒ Object



151
152
153
154
155
156
157
158
# File 'lib/nydp/pair.rb', line 151

def self.from_list list, last=nil, n=list.size-1
  while (n >= 0)
    last = cons(list[n], last)
    n -= 1
  end

  last
end

.parse_list(list) ⇒ Object

this breaks everything even though it seems like the logical thing to do… def to_ary

to_a

end



143
144
145
146
147
148
149
# File 'lib/nydp/pair.rb', line 143

def self.parse_list list
  if list.slice(-2) == :"."
    from_list(list[0...-2], list.slice(-1))
  else
    from_list list
  end
end

Instance Method Details

#&(other) ⇒ Object



24
# File 'lib/nydp/pair.rb', line 24

def &    other ; self.class.from_list((Set.new(self) & Array(other)).to_a)    ; end

#+(other) ⇒ Object



20
# File 'lib/nydp/pair.rb', line 20

def +    other ; copy_append other                          ; end

#-(other) ⇒ Object



26
# File 'lib/nydp/pair.rb', line 26

def -    other ; self.class.from_list((Set.new(self) - Array(other)).to_a)    ; end

#==(other) ⇒ Object



160
161
162
# File 'lib/nydp/pair.rb', line 160

def == other
  (NIL != other) && (other.respond_to? :car) && (self.car == other.car) && (self.cdr == other.cdr)
end

#[](i) ⇒ Object



23
# File 'lib/nydp/pair.rb', line 23

def []       i ; nth i                                      ; end

#caarObject



13
# File 'lib/nydp/pair.rb', line 13

def caar       ; car.car                                    ; end

#cadrObject



14
# File 'lib/nydp/pair.rb', line 14

def cadr       ; cdr.car                                    ; end

#cdarObject



15
# File 'lib/nydp/pair.rb', line 15

def cdar       ; car.cdr                                    ; end

#cddrObject



16
# File 'lib/nydp/pair.rb', line 16

def cddr       ; cdr.cdr                                    ; end

#compile_to_ruby(indent, srcs, opts = nil) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/nydp/pair.rb', line 85

def compile_to_ruby indent, srcs, opts=nil
  a,x = [], self
  while x.is_a?(Nydp::Pair)
    a << x.car.compile_to_ruby("", srcs)
    x = x.cdr
  end

  if !x || (x.is_a?(Nydp::Literal) && !x.expression)
    "#{indent}list(" + a.join(", ") + ")"
  else
    "#{indent}Nydp::Pair.from_list([" + a.join(", ") + "], #{x.compile_to_ruby "", srcs})"
  end
end

#copyObject



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/nydp/pair.rb', line 45

def copy
  a    = last = cons
  x    = self
  while (x.is_a? Nydp::Pair)
    last = last.cdr = cons(x.car)
    x    = x.cdr
  end

  last.cdr = x
  a.cdr
end

#copy_append(lastcdr) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/nydp/pair.rb', line 57

def copy_append lastcdr
  a    = last = cons
  x    = self
  while (x.is_a? Nydp::Pair)
    last = last.cdr = cons(x.car)
    x    = x.cdr
  end

  last.cdr = lastcdr
  a.cdr
end

#each(&block) ⇒ Object



164
165
166
167
168
169
170
# File 'lib/nydp/pair.rb', line 164

def each &block
  xs = self
  while xs
    yield xs.car
    xs = xs.cdr
  end
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


19
# File 'lib/nydp/pair.rb', line 19

def eql? other ; self == other                              ; end

#hashObject

can’t cache hash of symbol, breaks when unmarshalling



34
35
36
37
38
39
40
41
42
43
# File 'lib/nydp/pair.rb', line 34

def hash
  a    = 0
  x    = self
  while (x.is_a? Nydp::Pair)
    a = a + x.car.hash
    x = x.cdr
  end

  a + x.hash
end

#index_of(x) ⇒ Object



107
108
109
110
111
112
113
114
115
# File 'lib/nydp/pair.rb', line 107

def index_of x
  if x == car
    0
  elsif pair?(cdr)
    1 + cdr.index_of(x)
  else
    nil
  end
end

#inspectObject



22
# File 'lib/nydp/pair.rb', line 22

def inspect    ; "(#{inspect_rest})"                        ; end

#inspect_restObject



238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'lib/nydp/pair.rb', line 238

def inspect_rest
  res = [car._nydp_inspect]
  it = cdr
  while it
    if it.is_a?(self.class)
      res << it.car._nydp_inspect
      it = it.cdr
    else
      res << "."
      res << it._nydp_inspect
      it = nil
    end
  end
  res.compact.join " "
end

#join(str) ⇒ Object



81
82
83
# File 'lib/nydp/pair.rb', line 81

def join str
  to_a.join(str)
end

#mapObject



69
70
71
72
73
74
75
76
77
78
79
# File 'lib/nydp/pair.rb', line 69

def map
  a    = last = cons
  x    = self
  while (x.is_a? Nydp::Pair)
    last = last.cdr = cons(yield x.car)
    x    = x.cdr
  end

  last.cdr = yield(x) if x
  a.cdr
end

#nth(n) ⇒ Object



99
100
101
102
103
104
105
# File 'lib/nydp/pair.rb', line 99

def nth n
  xs = self
  while n > 0
    xs, n = xs.cdr, n-1
  end
  xs.car
end

#nydp_typeObject



12
# File 'lib/nydp/pair.rb', line 12

def nydp_type  ; :pair                                      ; end

#proper?Boolean

Returns:

  • (Boolean)


27
# File 'lib/nydp/pair.rb', line 27

def proper?    ; (!cdr) || (cdr.is_a?(Nydp::Pair) && cdr.proper?)             ; end

#sizeObject



21
# File 'lib/nydp/pair.rb', line 21

def size       ; 1 + (cdr.is_a?(Nydp::Pair) ? cdr.size : 0) ; end

#to_a(list = []) ⇒ Object

returns Array of elements as they are



129
130
131
132
133
134
135
136
# File 'lib/nydp/pair.rb', line 129

def to_a list=[]
  x = self
  while x.is_a?(Nydp::Pair)
    list << x.car
    x = x.cdr
  end
  list
end

#to_ruby(list = [], pair = self) ⇒ Object

returns Array of elements after calling #n2r on each element



118
119
120
121
122
123
124
125
126
# File 'lib/nydp/pair.rb', line 118

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

#to_sObject



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/nydp/pair.rb', line 172

def to_s
  if (car == :quote)
    if !cdr.cdr
      "'#{cdr.car.to_s}"
    else
      "'#{cdr.to_s}"
    end
  elsif (car == :"brace-list")
    if !cdr
      "{}"
    else
      "{ #{cdr.to_s_rest} }"
    end
  elsif (car == :"percent-syntax")
    cdr.to_a.compact.join("%")
  elsif (car == :"colon-syntax")
    cdr.to_a.compact.join(":")
  elsif (car == :"dot-syntax")
    cdr.to_a.compact.join(".")
  elsif (car == :"prefix-list")
    "#{cdr.car.to_s}#{cdr.cdr.car.to_s}"
  elsif (car == :quasiquote)
    if !cdr.cdr
      "`#{cdr.car.to_s}"
    else
      "`#{cdr.to_s}"
    end
  elsif (car == :unquote)
    if !cdr.cdr
      ",#{cdr.car.to_s}"
    else
      ",#{cdr.to_s}"
    end
  elsif (car == :"unquote-splicing")
    if !cdr.cdr
      ",@#{cdr.car.to_s}"
    else
      ",@#{cdr.to_s}"
    end
  else
    "(#{to_s_rest})"
  end
end

#to_s_carObject



216
217
218
219
220
221
222
223
224
225
226
# File 'lib/nydp/pair.rb', line 216

def to_s_car
  if (!car)
    "nil"
  elsif car.is_a?(String)
    car.inspect
  elsif car.is_a?(Symbol)
    car._nydp_inspect
  else
    car.to_s
  end
end

#to_s_restObject



228
229
230
231
232
233
234
235
236
# File 'lib/nydp/pair.rb', line 228

def to_s_rest
  cdr_s = if cdr.is_a?(self.class)
            cdr.to_s_rest
          elsif cdr
            ". #{cdr.to_s}"
          end

  [to_s_car, cdr_s].compact.join " "
end

#|(other) ⇒ Object



25
# File 'lib/nydp/pair.rb', line 25

def |    other ; self.class.from_list((Set.new(self) | Array(other)).to_a)    ; end