Class: Immutable::Cons

Inherits:
List
  • Object
show all
Defined in:
lib/immutable/list.rb

Overview

Immutable::Cons represents a cons cell.

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, #map, #null?, #reverse, #subsequences, #to_list, unfoldr

Methods included from Foldable

#length, #product, #sum

Constructor Details

#initialize(head, tail = Nil) ⇒ Cons

Creates a list obtained by prepending head to the list tail.



376
377
378
379
# File 'lib/immutable/list.rb', line 376

def initialize(head, tail = Nil)
  @head = head
  @tail = tail
end

Instance Attribute Details

#headObject (readonly)

Returns the value of attribute head.



385
386
387
# File 'lib/immutable/list.rb', line 385

def head
  @head
end

#tailObject (readonly)

Returns the value of attribute tail.



403
404
405
# File 'lib/immutable/list.rb', line 403

def tail
  @tail
end

Class Method Details

.[](head, tail = Nil) ⇒ Cons

Creates a new list obtained by prepending head to the list tail.

Returns:

  • (Cons)

    the new list.



371
372
373
# File 'lib/immutable/list.rb', line 371

def self.[](head, tail = Nil)
  self.new(head, tail)
end

Instance Method Details

#==(xs) ⇒ Object



473
474
475
476
477
478
479
# File 'lib/immutable/list.rb', line 473

def ==(xs)
  if !xs.is_a?(List) || xs.empty?
    false
  else
    @head == xs.head && @tail == xs.tail
  end
end

#[](n) ⇒ Object



535
536
537
538
539
540
541
542
543
# File 'lib/immutable/list.rb', line 535

def [](n)
  if n < 0
    nil
  elsif n == 0
    head
  else
    tail[n - 1]
  end
end

#drop(n) ⇒ Object



573
574
575
576
577
578
579
# File 'lib/immutable/list.rb', line 573

def drop(n)
  if n > 0
    @tail.drop(n - 1)
  else
    self
  end
end

#drop_while(&block) ⇒ Object



585
586
587
588
589
590
591
# File 'lib/immutable/list.rb', line 585

def drop_while(&block)
  if yield(@head)
    @tail.drop_while(&block)
  else
    self
  end
end

#each {|@head| ... } ⇒ Object

Yields:



448
449
450
451
# File 'lib/immutable/list.rb', line 448

def each(&block)
  yield(@head)
  @tail.each(&block)
end

#empty?Boolean

Returns:

  • (Boolean)


421
422
423
# File 'lib/immutable/list.rb', line 421

def empty?
  false
end

#find(&block) ⇒ Object



597
598
599
600
601
602
603
# File 'lib/immutable/list.rb', line 597

def find(&block)
  if yield(@head)
    @head
  else
    @tail.find(&block)
  end
end

#foldl(e, &block) ⇒ Object



457
458
459
# File 'lib/immutable/list.rb', line 457

def foldl(e, &block)
  @tail.foldl(yield(e, @head), &block)
end

#foldl1(&block) ⇒ Object



465
466
467
# File 'lib/immutable/list.rb', line 465

def foldl1(&block)
  @tail.foldl(@head, &block)
end

#foldr(e) {|@head, @tail.foldr(e, &block)| ... } ⇒ Object

Yields:



429
430
431
# File 'lib/immutable/list.rb', line 429

def foldr(e, &block)
  yield(@head, @tail.foldr(e, &block))
end

#foldr1(&block) ⇒ Object



437
438
439
440
441
442
443
# File 'lib/immutable/list.rb', line 437

def foldr1(&block)
  if @tail.empty?
    @head
  else
    yield(@head, @tail.foldr1(&block))
  end
end

#initObject



409
410
411
412
413
414
415
# File 'lib/immutable/list.rb', line 409

def init
  if @tail.empty?
    Nil
  else
    Cons[@head, @tail.init]
  end
end

#inspectObject



485
486
487
488
# File 'lib/immutable/list.rb', line 485

def inspect
  "List[" + @head.inspect +
    @tail.foldl("") {|x, y| x + ", " + y.inspect } + "]"
end

#intersperse(sep) ⇒ Object



494
495
496
# File 'lib/immutable/list.rb', line 494

def intersperse(sep)
  Cons[@head, @tail.prepend_to_all(sep)]
end

#lastObject



391
392
393
394
395
396
397
# File 'lib/immutable/list.rb', line 391

def last
  if @tail.empty?
    @head
  else
    @tail.last
  end
end

#nonempty_subsequencesObject



524
525
526
527
528
529
# File 'lib/immutable/list.rb', line 524

def nonempty_subsequences
  yss = @tail.nonempty_subsequences.foldr(List[]) { |xs, xss|
    Cons[xs, Cons[Cons[@head, xs], xss]]
  }
  Cons[List[@head], yss]
end

#prepend_to_all(sep) ⇒ Object



502
503
504
# File 'lib/immutable/list.rb', line 502

def prepend_to_all(sep)
  Cons[sep, Cons[@head, @tail.prepend_to_all(sep)]]
end

#take(n) ⇒ Object



549
550
551
552
553
554
555
# File 'lib/immutable/list.rb', line 549

def take(n)
  if n <= 0
    Nil
  else
    Cons[@head, @tail.take(n - 1)]
  end
end

#take_while(&block) ⇒ Object



561
562
563
564
565
566
567
# File 'lib/immutable/list.rb', line 561

def take_while(&block)
  if yield(@head)
    Cons[@head, @tail.take_while(&block)]
  else
    Nil
  end
end

#transposeObject



510
511
512
513
514
515
516
517
518
# File 'lib/immutable/list.rb', line 510

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

#zip(*xss) ⇒ Object



609
610
611
612
613
# File 'lib/immutable/list.rb', line 609

def zip(*xss)
  heads = xss.map { |xs| xs.null? ? nil : xs.head }
  tails = xss.map { |xs| xs.null? ? Nil : xs.tail }
  Cons[[head, *heads], tail.zip(*tails)]
end

#zip_with(*xss, &block) ⇒ Object



619
620
621
622
623
# File 'lib/immutable/list.rb', line 619

def zip_with(*xss, &block)
  heads = xss.map { |xs| xs.null? ? nil : xs.head }
  tails = xss.map { |xs| xs.null? ? Nil : xs.tail }
  Cons[yield(head, *heads), tail.zip_with(*tails, &block)]
end