2015-12-16 00:07:31 -05:00
|
|
|
# frozen_string_literal: false
|
2013-07-09 19:21:36 -04:00
|
|
|
module Gem
|
|
|
|
class List
|
2015-07-01 17:50:14 -04:00
|
|
|
include Enumerable
|
|
|
|
attr_accessor :value, :tail
|
|
|
|
|
|
|
|
def initialize(value = nil, tail = nil)
|
|
|
|
@value = value
|
|
|
|
@tail = tail
|
|
|
|
end
|
|
|
|
|
2013-07-09 19:21:36 -04:00
|
|
|
def each
|
|
|
|
n = self
|
|
|
|
while n
|
|
|
|
yield n.value
|
|
|
|
n = n.tail
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def to_a
|
2015-07-01 17:50:14 -04:00
|
|
|
super.reverse
|
2013-07-09 19:21:36 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def prepend(value)
|
|
|
|
List.new value, self
|
|
|
|
end
|
|
|
|
|
2013-10-19 20:31:12 -04:00
|
|
|
def pretty_print q # :nodoc:
|
|
|
|
q.pp to_a
|
|
|
|
end
|
|
|
|
|
2013-07-09 19:21:36 -04:00
|
|
|
def self.prepend(list, value)
|
|
|
|
return List.new(value) unless list
|
|
|
|
List.new value, list
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|