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



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

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

#empty_listObject



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

def empty_list
  Nil
end

#enum_to_list(enum) ⇒ Object



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

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



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

def head(p)
  p.first
end

#list_to_enum(list) ⇒ Object



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

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

#tail(p) ⇒ Object



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

def tail(p)
  p.last
end