From: Rodrigo Rosenfeld Rosas Date: 2012-02-14T05:47:32+09:00 Subject: [ruby-core:42570] [ruby-trunk - Feature #4890] Enumerable#lazy Issue #4890 has been updated by Rodrigo Rosenfeld Rosas. Thomas, continuing, if the "force" approach is taken instead of take(n), you'll still have to call it (or call each/select/to_a) in the end, as oppose to what happens if you just call map/collect, for example. It can also lead to unexpected behavior. For example, on this case, the side-effect won't happen on some elements: items.map {|item| this_should_always_be_done_for_each(item) }.select{item.index == 2} If "items" is lazy by default, "this_should_always_be_done_for_each" won't be called for all "items" unless only the last one has index 2. ---------------------------------------- Feature #4890: Enumerable#lazy https://bugs.ruby-lang.org/issues/4890 Author: Yutaka HARA Status: Assigned Priority: Normal Assignee: Yutaka HARA Category: core Target version: 2.0.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://bugs.ruby-lang.org/