Lazy.new(obj, size=nil) { |yielder, *values| ... }
Class Public methods
Creates a new Lazy enumerator. When the enumerator
is actually enumerated (e.g. by calling force), obj
will be
enumerated and each value passed to the given block. The block can yield
values back using yielder
. For example, to create a method
filter_map
in both lazy and non-lazy fashions:
module Enumerable def filter_map(&block) map(&block).compact end end class Enumerator::Lazy def filter_map Lazy.new(self) do |yielder, *values| result = yield *values yielder << result if result end end end (1..Float::INFINITY).lazy.filter_map{|i| i*i if i.even?}.first(5) # => [4, 16, 36, 64, 100]
Please login to continue.