Project

General

Profile

Actions

Feature #15901

closed

Enumerator::Lazy#eager

Added by knu (Akinori MUSHA) over 5 years ago. Updated about 5 years ago.

Status:
Closed
Target version:
-
[ruby-core:92978]

Description

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.

/*
 * 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

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0