From: Akinori MUSHA Date: 2011-10-31T17:52:17+09:00 Subject: [ruby-core:40552] [ruby-trunk - Feature #4890] Enumerable#lazy Issue #4890 has been updated by Akinori MUSHA. My suggestion is to turn Enumerator into a lazy stream and make every Enumerable class including the new Enumerator operate on the receiver's class wherever appropriate instead of fixing Array as result container. For example, Hash#select would return a hash, Set#reject would return a set, and so on. If the result of a method does not fit in the receiver class (Set#sort may be a good example) it can return an enumerator or an inevitable array instead. Adding extra methods to Enumerable/Enumerator is just another story for keeping backward compatibility as much as possible. I do not insist on 2.0 as the target at all. I just feel that we can work out a better, seamless solution than the plugin approach that can be accomplished outside of the core. ---------------------------------------- Feature #4890: Enumerable#lazy http://redmine.ruby-lang.org/issues/4890 Author: Yutaka HARA Status: Open Priority: Normal Assignee: Yukihiro Matsumoto Category: core Target version: 2.0 =begin = Example Print first 100 primes which are in form of n^2+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 (()) (also attached to this ticket) =end -- http://redmine.ruby-lang.org