Issue #4890 has been updated by Shugo Maeda.


Akinori MUSHA wrote:
> 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.

It sounds fine for me, but I don't think Enumerable (or Enumerator) should have methods of Array such as [] and size.

> 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.

FYI, Hash#select returns a Hash in Ruby 1.9.
Before Ruby 1.9 Matz rejected requests to change the resulting values of Enumerable methods,
but he might have changed his mind.
Anyway, I'd like to hear his opinion.

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

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

=end



-- 
http://redmine.ruby-lang.org