Issue #7248 has been updated by yhara (Yutaka HARA).

Assignee changed from yhara (Yutaka HARA) to marcandre (Marc-Andre Lafortune)

marcandre (Marc-Andre Lafortune) wrote:
> Here's what I propose as the official Lazy.new documentation and API:
> 
>     Lazy.new(obj, size=nil) { |yielder, *values| ... }
> 
>     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]
> 
> Does this seem acceptable to you?
> 
Yes!
----------------------------------------
Bug #7248: Shouldn't Enumerator::Lazy.new be private?
https://bugs.ruby-lang.org/issues/7248#change-35448

Author: marcandre (Marc-Andre Lafortune)
Status: Assigned
Priority: High
Assignee: marcandre (Marc-Andre Lafortune)
Category: core
Target version: 2.0.0
ruby -v: ruby 2.0.0dev (2012-10-29 trunk 37380) [x86_64-darwin10.8.0]


Is there a reason why Enumerator::Lazy.new is not private?

Lazy enumerators should be created with `Enumerable#lazy`. Moreover, there is no doc, and it can give unexpected results too.


-- 
http://bugs.ruby-lang.org/