Issue #15901 has been updated by marcandre (Marc-Andre Lafortune).


knu (Akinori MUSHA) wrote:
> > I'd also alias Enumerator#eager to #itself.
> 
> Could you elaborate on why?  Are you thinking of situations where lazy and eager enumerators are mixed?

Yes, I was thinking of a method accepting a potentially lazy enumerator. It would be to avoid having to write `enum = enum.eager if enum.respond_to?(:eager)` and simply calling `enum.eager` no matter if the argument is eager or not.

It's in the same way as `Enumerator::Lazy#lazy` is an alias to `itself`.

I don't except it to be very useful though, lazy enumerators are probably rare and not often mixed with eager enumerators, so it might be premature. I just think the extra cognitive load is basically zero.


----------------------------------------
Feature #15901: Enumerator::Lazy#eager
https://bugs.ruby-lang.org/issues/15901#change-81369

* Author: knu (Akinori MUSHA)
* Status: Open
* Priority: Normal
* Assignee: matz (Yukihiro Matsumoto)
* Target version: 
----------------------------------------
There are cases where you want to create and pass a normal Enumerable object to a consumer where the methods like map and select are expected to return an array, but the calculation would be so space costly without using Enumerator::Lazy because of intermediate arrays.  In such cases, you would want to chain `lazy` and calculation methods like `flat_map` and `select`, then convert the lazy enumerator back to a normal Enumerator.

However, there is no direct method that converts a lazy Enumerator to an eager one, because the` to_enum` method returns a lazy Enumerator when the receiver is a lazy Enumerator.  So, I propose this `eager` method as the missing piece for the said use case.

Here's the rdoc from the attached patch.

```C
/*
 * call-seq:
 *   lzy.eager -> enum
 *
 * Returns a non-lazy Enumerator converted from the lazy enumerator.
 *
 * This is useful where a normal Enumerable object needs to be
 * generated while lazy operation is still desired to avoid creating
 * intermediate arrays.
 *
 *   enum = huge_collection.lazy.flat_map(&:children).reject(&:disabled?).eager
 *   enum.map {|x| ...}  # an array is returned
 */
```

---Files--------------------------------
0001-Implement-Enumerator-Lazy-eager.patch (2.32 KB)


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

Unsubscribe: <mailto:ruby-core-request / ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>