Class: Seq::Lazy
Overview
Lazy seqs evaluate a block to determine the next value when needed, this value is then saved.
Constant Summary
Constants inherited from Seq
Instance Method Summary collapse
-
#initialize(list = [], items = Float::INFINITY, offset = 0, default = nil) {|list| ... } ⇒ Lazy
constructor
Creates a new Lazy seq instance.
-
#next ⇒ Object
Until ended it returns the next item from
list
if it exists or calculates it then stores it inlist
, if ended it returns the default value. -
#reset ⇒ Object
Resets the state of the lazy seq.
Methods inherited from Seq
#each, #ended?, #entries, #inc, #infinite?, #method_missing, #to_a
Constructor Details
#initialize(list = [], items = Float::INFINITY, offset = 0, default = nil) {|list| ... } ⇒ Lazy
Creates a new Lazy seq instance.
32 33 34 35 36 37 38 39 40 |
# File 'lib/seq/lazy.rb', line 32 def initialize(list=[], items=Float::INFINITY, offset=0, default=nil, &block) @list = list @items = items @block = block @offset = offset @default = default self.reset end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class Seq
Instance Method Details
#next ⇒ Object
Returns Until ended it returns the next item from list
if it exists or calculates it then stores it in list
, if ended it returns the default value.
59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/seq/lazy.rb', line 59 def next if ended? @default else @index += 1 if @index-1 < @list.size @list[@index-1] else @list[@index-1] = @block.call(@list) end end end |
#reset ⇒ Object
Resets the state of the lazy seq. It also calculates any values necessary to get to the offset.
44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/seq/lazy.rb', line 44 def reset @index = @list.size @cycles = 0 until @list.size >= @offset @list[@index] = @block.call(@list[0..@index-1]) @index += 1 end @index = @offset end |