Skip to content

Commit

Permalink
Specialize List methods for EmptyList.
Browse files Browse the repository at this point in the history
  • Loading branch information
Scott Olson committed Apr 22, 2013
1 parent e7cef54 commit 9ac3b23
Showing 1 changed file with 18 additions and 7 deletions.
25 changes: 18 additions & 7 deletions lib/apricot/list.rb
Expand Up @@ -15,18 +15,14 @@ def self.[](*args)

def initialize(head, tail)
@head = head
@tail = tail
@tail = tail || EMPTY_LIST
@count = tail ? tail.count + 1 : 1
end

def cons(x)
List.new(x, self)
end

def empty?
!@tail
end

def initialize_copy(other)
super
@tail = other.tail.dup if other.tail && !other.tail.empty?
Expand All @@ -39,15 +35,15 @@ def to_list
end

def first
empty? ? nil : @head
@head
end

def next
@tail.empty? ? nil : @tail
end

def to_seq
empty? ? nil : self
self
end

def inspect
Expand All @@ -65,6 +61,21 @@ class EmptyList < List
def initialize
@count = 0
end

def each
end

def empty?
true
end

def first
nil
end

def next
nil
end
end

EMPTY_LIST = EmptyList.new
Expand Down

0 comments on commit 9ac3b23

Please sign in to comment.