class Enumerable::Lazy
Public Class Methods
new(obj, &block)
click to toggle source
Calls superclass method
# File lib/enumerable/lazy.rb, line 36 def initialize(obj, &block) super(){|yielder| begin obj.each{|x| if block block.call(yielder, x) else yielder << x end } rescue StopIteration end } end
Public Instance Methods
drop(n)
click to toggle source
# File lib/enumerable/lazy.rb, line 83 def drop(n) dropped = 0 Lazy.new(self){|yielder, val| if dropped < n dropped += 1 else yielder << val end } end
drop_while(&block)
click to toggle source
# File lib/enumerable/lazy.rb, line 94 def drop_while(&block) dropping = true Lazy.new(self){|yielder, val| if dropping if not block.call(val) yielder << val dropping = false end else yielder << val end } end
flat_map(&block)
click to toggle source
# File lib/enumerable/lazy.rb, line 130 def flat_map(&block) Lazy.new(self){|yielder, val| ary = block.call(val) # TODO: check ary is an Array ary.each{|x| yielder << x } } end
Also aliased as: collect_concat
grep(pattern)
click to toggle source
# File lib/enumerable/lazy.rb, line 75 def grep(pattern) Lazy.new(self){|yielder, val| if pattern === val yielder << val end } end
map(&block)
click to toggle source
# File lib/enumerable/lazy.rb, line 51 def map(&block) Lazy.new(self){|yielder, val| yielder << block.call(val) } end
Also aliased as: collect
reject(&block)
click to toggle source
# File lib/enumerable/lazy.rb, line 67 def reject(&block) Lazy.new(self){|yielder, val| if not block.call(val) yielder << val end } end
select(&block)
click to toggle source
# File lib/enumerable/lazy.rb, line 58 def select(&block) Lazy.new(self){|yielder, val| if block.call(val) yielder << val end } end
Also aliased as: find_all
take(n)
click to toggle source
# File lib/enumerable/lazy.rb, line 108 def take(n) taken = 0 Lazy.new(self){|yielder, val| if taken < n yielder << val taken += 1 else raise StopIteration end } end
take_while(&block)
click to toggle source
# File lib/enumerable/lazy.rb, line 120 def take_while(&block) Lazy.new(self){|yielder, val| if block.call(val) yielder << val else raise StopIteration end } end
zip(*args, &block)
click to toggle source
# File lib/enumerable/lazy.rb, line 141 def zip(*args, &block) enums = [self] + args Lazy.new(self){|yielder, val| ary = enums.map{|e| e.next} if block yielder << block.call(ary) else yielder << ary end } end