Project

General

Profile

Feature #4890

Enumerable#lazy

Added by yhara (Yutaka HARA) about 9 years ago. Updated over 8 years ago.

Status:
Closed
Priority:
Normal
Target version:
[ruby-core:37164]

Description

=begin
= Example
Print first 100 primes which are in form of n2+1

require 'prime'
INFINITY = 1.0 / 0
p (1..INFINITY).lazy.map{|n| n**2+1}.select{|m| m.prime?}.take(100)

(Example taken from enumerable_lz; thanks @antimon2)

= Description

Enumerable#lazy returns an instance of Enumerable::Lazy.
This is the only method added to the existing bulit-in classes.

Lazy is a subclass of Enumerator, which includes Enumerable.
So you can call any methods of Enumerable on Lazy, except methods like
map, select, etc. are redefined as 'lazy' versions.

= Sample implementation

((URL:https://gist.github.com/1028609))
(also attached to this ticket)

=end


Files

lazy.rb (3.85 KB) lazy.rb yhara (Yutaka HARA), 06/16/2011 07:23 PM

Related issues

Related to Ruby master - Feature #4653: [PATCH 1/1] new method Enumerable#rude_mapRejectedmatz (Yukihiro Matsumoto)05/08/2011Actions
Related to Ruby master - Feature #708: Lazy Enumerator#select, Enumerator#map etc.Rejectedmatz (Yukihiro Matsumoto)Actions
Related to Ruby master - Bug #7248: Shouldn't Enumerator::Lazy.new be private?Closedmarcandre (Marc-Andre Lafortune)10/31/2012Actions

Also available in: Atom PDF