Module: Abstractivator::Cons

Extended by:
Cons
Included in:
Cons
Defined in:
lib/abstractivator/cons.rb

Defined Under Namespace

Classes: Cell

Constant Summary collapse

Nil =
Object.new

Instance Method Summary collapse

Instance Method Details

#cons(h, t) ⇒ Object



10
11
12
# File 'lib/abstractivator/cons.rb', line 10

def cons(h, t)
  Cell.new(h, t)
end

#empty_listObject



6
7
8
# File 'lib/abstractivator/cons.rb', line 6

def empty_list
  Nil
end

#enum_to_list(enum) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/abstractivator/cons.rb', line 31

def enum_to_list(enum)
  e = enum.reverse.each
  result = Nil
  begin
    while true
      result = cons(e.next, result)
    end
  rescue StopIteration
    result
  end
end

#head(p) ⇒ Object



14
15
16
# File 'lib/abstractivator/cons.rb', line 14

def head(p)
  p.first
end

#list_to_enum(list) ⇒ Object



22
23
24
25
26
27
28
29
# File 'lib/abstractivator/cons.rb', line 22

def list_to_enum(list)
  Enumerator.new do |y|
    while list != Nil
      y << head(list)
      list = tail(list)
    end
  end
end

#tail(p) ⇒ Object



18
19
20
# File 'lib/abstractivator/cons.rb', line 18

def tail(p)
  p.last
end