Issue #4890 has been updated by Shugo Maeda.


Akinori MUSHA wrote:
> > It sounds fine for me, but I don't think Enumerable (or Enumerator) should have methods of Array such as [] and size.
> 
> It'll be OK if once we decide we don't care too much about backward compatibility in 3.0.
> Though I think it would be nice if we can add attributes (aspects) like indexability and finiteness so the wrapper module (Enumerable) can take advance of them and enable [] and size as appropriate.

It's confusing, isn't it?

If your proposal is accepted, I want Scala-like force, which returns an instance of the original collection class.

  scala> List(1,2,3).view.map(_ + 1).filter(_ % 2 == 0).force
  res0: Seq[Int] = List(2, 4)
  
  scala> Set(1,2,3).view.map(_ + 1).filter(_ % 2 == 0).force
  res1: Iterable[Int] = Set(2, 4)

So there is no reason to treat Array specially, except compatibility reason.
If you need an Array, you can call to_a explicitly.

> > How about to add Enumerable#defer that returns a lazy version of Enumerator as a transition step in Ruby 2.0?
> > If Enumerator gets lazy in Ruby 3.0, Enumerable#defer can be changed to be just an alias of to_enum.
> 
> Is defer the new name for lazy in this proposal?

The name may be changed.  I'd like to hear Matz's 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: 3.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