Class: Immutable::Cons
- Inherits:
-
List
- Object
- List
- Immutable::Cons
show all
- Defined in:
- lib/immutable/list.rb
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Methods inherited from List
#+, #filter, #first, #flat_map, #flatten, from_array, from_enum, #intercalate, #length, #map, #null?, #product, #reverse, #sum, unfoldr
Constructor Details
#initialize(head, tail = Nil) ⇒ Cons
Creates a list obtained by prepending head to the list tail.
263
264
265
266
|
# File 'lib/immutable/list.rb', line 263
def initialize(head, tail = Nil)
@head = head
@tail = tail
end
|
Instance Attribute Details
#head ⇒ Object
Returns the value of attribute head.
272
273
274
|
# File 'lib/immutable/list.rb', line 272
def head
@head
end
|
#tail ⇒ Object
Returns the value of attribute tail.
290
291
292
|
# File 'lib/immutable/list.rb', line 290
def tail
@tail
end
|
Class Method Details
.[](head, tail = Nil) ⇒ Object
Creates a list obtained by prepending head to the list tail.
258
259
260
|
# File 'lib/immutable/list.rb', line 258
def self.[](head, tail = Nil)
self.new(head, tail)
end
|
Instance Method Details
#==(xs) ⇒ Object
348
349
350
351
352
353
354
|
# File 'lib/immutable/list.rb', line 348
def ==(xs)
if xs.empty?
false
else
@head == xs.head && @tail == xs.tail
end
end
|
#drop(n) ⇒ Object
423
424
425
426
427
428
429
|
# File 'lib/immutable/list.rb', line 423
def drop(n)
if n > 0
@tail.drop(n - 1)
else
self
end
end
|
#drop_while(&block) ⇒ Object
435
436
437
438
439
440
441
|
# File 'lib/immutable/list.rb', line 435
def drop_while(&block)
if yield(@head)
@tail.drop_while(&block)
else
self
end
end
|
#empty? ⇒ Boolean
308
309
310
|
# File 'lib/immutable/list.rb', line 308
def empty?
false
end
|
#find(&block) ⇒ Object
447
448
449
450
451
452
453
|
# File 'lib/immutable/list.rb', line 447
def find(&block)
if yield(@head)
@head
else
@tail.find(&block)
end
end
|
#foldl(e, &block) ⇒ Object
336
337
338
|
# File 'lib/immutable/list.rb', line 336
def foldl(e, &block)
@tail.foldl(yield(e, @head), &block)
end
|
#foldl1(&block) ⇒ Object
344
345
346
|
# File 'lib/immutable/list.rb', line 344
def foldl1(&block)
@tail.foldl(@head, &block)
end
|
#foldr(e) {|@head, @tail.foldr(e, &block)| ... } ⇒ Object
316
317
318
|
# File 'lib/immutable/list.rb', line 316
def foldr(e, &block)
yield(@head, @tail.foldr(e, &block))
end
|
#foldr1(&block) ⇒ Object
324
325
326
327
328
329
330
|
# File 'lib/immutable/list.rb', line 324
def foldr1(&block)
if @tail.empty?
@head
else
yield(@head, @tail.foldr1(&block))
end
end
|
#init ⇒ Object
296
297
298
299
300
301
302
|
# File 'lib/immutable/list.rb', line 296
def init
if @tail.empty?
Nil
else
Cons[@head, @tail.init]
end
end
|
#inspect ⇒ Object
360
361
362
363
|
# File 'lib/immutable/list.rb', line 360
def inspect
"List[" + @head.inspect +
@tail.foldl("") {|x, y| x + ", " + y.inspect } + "]"
end
|
#intersperse(sep) ⇒ Object
369
370
371
|
# File 'lib/immutable/list.rb', line 369
def intersperse(sep)
Cons[@head, @tail.prepend_to_all(sep)]
end
|
#last ⇒ Object
278
279
280
281
282
283
284
|
# File 'lib/immutable/list.rb', line 278
def last
if @tail.empty?
@head
else
@tail.last
end
end
|
#prepend_to_all(sep) ⇒ Object
377
378
379
|
# File 'lib/immutable/list.rb', line 377
def prepend_to_all(sep)
Cons[sep, Cons[@head, @tail.prepend_to_all(sep)]]
end
|
#take(n) ⇒ Object
399
400
401
402
403
404
405
|
# File 'lib/immutable/list.rb', line 399
def take(n)
if n <= 0
Nil
else
Cons[@head, @tail.take(n - 1)]
end
end
|
#take_while(&block) ⇒ Object
411
412
413
414
415
416
417
|
# File 'lib/immutable/list.rb', line 411
def take_while(&block)
if yield(@head)
Cons[@head, @tail.take_while(&block)]
else
Nil
end
end
|
#transpose ⇒ Object
385
386
387
388
389
390
391
392
393
|
# File 'lib/immutable/list.rb', line 385
def transpose
if @head == Nil
@tail.transpose
else
tail = @tail.filter { |x| !x.empty? }
Cons[Cons[@head.head, tail.map(&:head)],
Cons[@head.tail, tail.map(&:tail)].transpose]
end
end
|