Class: LazyList::Base

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/lazy_list/base.rb

Direct Known Subclasses

Stream

Instance Method Summary collapse

Constructor Details

#initialize(list = nil) ⇒ Base

Returns a new instance of Base.



5
6
7
8
# File 'lib/lazy_list/base.rb', line 5

def initialize(list=nil)
  @list = list
  @list ||= LazyList.cons
end

Instance Method Details

#each {|first| ... } ⇒ Object

Yields:



32
33
34
35
36
# File 'lib/lazy_list/base.rb', line 32

def each(&block)
  return if first.nil?
  yield first
  rest.each(&block)
end

#empty?Boolean

Returns:

  • (Boolean)


10
11
12
# File 'lib/lazy_list/base.rb', line 10

def empty?
  @has_contents ||= false
end

#firstObject



24
25
26
# File 'lib/lazy_list/base.rb', line 24

def first
  LazyList.car(@list).call
end

#inspectObject



14
15
16
# File 'lib/lazy_list/base.rb', line 14

def inspect
  "[" + (map { |element| element.inspect }.join(", ")) + "]"
end

#prepend(a) ⇒ Object



18
19
20
21
22
# File 'lib/lazy_list/base.rb', line 18

def prepend(a)
  @has_contents = true
  @list = LazyList.cons a, @list
  self
end

#restObject



28
29
30
# File 'lib/lazy_list/base.rb', line 28

def rest
  self.class.new LazyList.cdr(@list).call
end