From: zverok.offline@... Date: 2019-06-13T07:14:29+00:00 Subject: [ruby-core:93094] [Ruby trunk Feature#15901] Enumerator::Lazy#eager Issue #15901 has been updated by zverok (Victor Shepelev). > How about this style? ```ruby [0, 1, 2].lazy {|e| e.map {|n| n + 1 }.map {|n| n.to_s } } #=> an Enumerator containing "1", "2", and "3" ``` I believe that it defies the whole idea of lazy enumerators. Instead of chaining laziness (and having regular polymorphic enumerator, that can be passed into other methods, or returned from some methods to "external" clients, and so on), e.g. instead of lazy enums being "just enums", it requires wrapping everything in a `lazy {}` block, effectively limiting applicability of the idea. This approach ("everything inside the block is lazy-computed") _may_ be an optimization technique; but lazy enumerators currently is an idiom, even if underappreciated (especially ActiveSupport loves to switch from "generic enumerables" to "just arrays"), but very prominent and interesting. ---------------------------------------- Feature #15901: Enumerator::Lazy#eager https://bugs.ruby-lang.org/issues/15901#change-78516 * Author: knu (Akinori MUSHA) * Status: Open * Priority: Normal * Assignee: * 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: