2016-02-01 07:43:26 -05:00
|
|
|
# frozen_string_literal: true
|
2013-07-09 19:21:36 -04:00
|
|
|
module Gem
|
|
|
|
class List
|
2019-02-14 07:59:03 -05:00
|
|
|
|
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
|
|
|
|
|
2018-11-21 05:20:47 -05:00
|
|
|
def pretty_print(q) # :nodoc:
|
2013-10-19 20:31:12 -04:00
|
|
|
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
|
2019-02-14 07:59:03 -05:00
|
|
|
|
2013-07-09 19:21:36 -04:00
|
|
|
end
|
|
|
|
end
|