Project

General

Profile

Actions

Feature #21520

open

Feature Proposal: Enumerator::Lazy#lazy_each

Added by nuzair46 (Nuzair Rasheed) 9 days ago. Updated 3 days ago.

Status:
Open
Assignee:
-
Target version:
-
[ruby-core:122847]

Description

Abstract

Add a #lazy_each method to Enumerator::Lazy that allows observing each element in a lazy enumeration pipeline without modifying or consuming the stream.

Background

Ruby provides Enumerator::Lazy for efficient lazy stream processing. However, unlike languages such as Java, it lacks a clean way to inspect intermediate elements during lazy evaluation.

Currently, developers must misuse .map for side effects, example:

(1..).lazy.map { |x| puts x; x }.select(&:even?).first(3)

This is semantically incorrect and confusing, since .map implies transformation, not observation.

Proposal

Introduce Enumerator::Lazy#lazy_each, which yields each item to a block and returns the item unmodified, similar to Object#tap, but in a lazy stream:

(1..).lazy
     .lazy_each { |x| puts "saw: #{x}" }
     .select(&:even?)
     .first(3)

This would be equivalent to:

lazy.map { |x| block.call(x); x }

but with improved semantic clarity.

Use cases

• Debugging lazy enumerators without breaking the chain
• Logging or instrumentation in pipelines
• Educational / demo use for showing lazy evaluation step-by-step
• Cleaner replacement for map { puts x; x } hacks

Example:

data = (1..).lazy
            .lazy_each { |x| puts "got #{x}" }
            .select(&:even?)
            .first(5)

result:

got 1
got 2
got 3
got 4
got 5
...
got 10

And return [2, 4, 6, 8, 10]

Discussion

#lazy_each is a minimal, non-breaking addition that improves clarity and idiomatic usage of Enumerator::Lazy. It avoids abusing .map for observation and is familiar to developers from other languages. #lazy_each is also not needed for other enumerators where .tap or .each can do the job.

It mirrors Java’s .stream().peek(...) and makes Ruby’s lazy enumeration more expressive and readable.

See also

Java Stream.peek

I have a draft PR for the implementation ready https://github.com/ruby/ruby/pull/14024

Actions

Also available in: Atom PDF

Like0
Like0Like1Like0Like0Like0Like1Like0