Files
mruby-mruby/mrbgems/mruby-enum-lazy
Yukihiro "Matz" Matsumoto 0994d5be94 mruby-enum-lazy: add comprehensive call-seq documentation for lazy enumeration
Added complete call-seq documentation for all lazy enumeration methods in
mrblib/lazy.rb (16 methods):

## Enumerable Extension Methods:

- lazy: creates Enumerator::Lazy for deferred evaluation, enables efficient
  processing of infinite sequences and large datasets with comprehensive
  pythagorean triples example demonstrating real-world usage

## Enumerator::Lazy Class Methods:

- new: constructor for creating lazy enumerators with custom yielding logic,
  provides foundation for building custom lazy operations

- to_enum/enum_for: creates lazy enumerator from method calls, maintains
  lazy evaluation chain for custom enumerable methods

## Enumerator::Lazy Instance Methods:

- map/collect: lazy transformation of elements with deferred execution
- select/find_all: lazy filtering with conditional element inclusion
- reject: lazy filtering with conditional element exclusion
- grep: lazy pattern matching using case equality operator
- grep_v: lazy inverse pattern matching for exclusion filtering

- drop: lazy skipping of first n elements without immediate evaluation
- drop_while: lazy conditional skipping until predicate fails
- take: lazy limiting to first n elements with automatic termination
- take_while: lazy conditional taking until predicate fails

- flat_map/collect_concat: lazy flattening and mapping in single operation
- zip: lazy combining of multiple enumerables into tuples
- uniq: lazy uniqueness filtering with optional transformation block

- force: immediate evaluation alias for to_a, converts lazy chain to array

Co-authored-by: Atlassian Rovo Dev
2025-08-14 10:52:46 +09:00
..
2024-02-15 14:55:38 +09:00
2016-11-30 12:36:22 +09:00
2025-06-13 04:45:48 +09:00

mruby-enum-lazy

Overview

This mrbgem provides lazy evaluation for Enumerable objects in mruby. It introduces the Enumerable#lazy method, which returns an instance of Enumerator::Lazy. This allows for more efficient processing of collections, especially large or potentially infinite sequences, by evaluating elements only when they are needed.

Functionality

When you call .lazy on an Enumerable object (like an Array or Range), you get back an Enumerator::Lazy object. This object behaves much like a regular Enumerator, but with a key difference: methods that transform the collection are deferred until the results are actually required.

The following methods are implemented to act lazily:

  • map / collect
  • select / find_all
  • reject
  • grep
  • grep_v
  • drop
  • drop_while
  • take
  • take_while
  • flat_map / collect_concat
  • zip
  • uniq

To trigger the evaluation of the lazy operations and retrieve all results (if finite), you can use methods like force or to_a.

How it works

Operations on an Enumerator::Lazy object are chained together. The actual computation of each element is postponed until it's requested (e.g., by force, to_a, or iterating with each). This can lead to significant performance improvements by avoiding unnecessary computations and memory allocations, particularly when dealing with large data sets or when only a subset of results is needed.

Usage Example

Here's a simple example demonstrating lazy evaluation:

# Without lazy evaluation
# This would attempt to create an infinite array, which is not feasible.
# (1..Float::INFINITY).map { |x| x * x }.select { |x| x % 2 == 0 }.take(5).to_a

# With lazy evaluation
p (1..Float::INFINITY).lazy.map { |x| x * x }.select { |x| x % 2 == 0 }.take(5).force
# Output: [4, 16, 36, 64, 100]

# Another example:
a = [1, 2, 3, 4, 5]
lazy_sequence = a.lazy.map do |x|
  puts "mapping #{x}"
  x * 10
end.select do |x|
  puts "selecting #{x}"
  x > 20
end

puts "Applying force..."
result = lazy_sequence.force
# Output:
# mapping 1
# selecting 10
# mapping 2
# selecting 20
# mapping 3
# selecting 30
# mapping 4
# selecting 40
# mapping 5
# selecting 50
# Applying force...
p result # Output: [30, 40, 50]

Dependencies

This gem depends on the following mruby core gems:

  • mruby-enumerator
  • mruby-enum-ext

License

MIT License

Author

mruby developers

Acknowledgements

Based on https://github.com/yhara/enumerable-lazy Inspired by https://github.com/antimon2/enumerable_lz Reference: http://jp.rubyist.net/magazine/?0034-Enumerable_lz (ja)