From: Innokenty Mikhailov Date: 2012-02-25T08:07:18+09:00 Subject: [ruby-core:42873] [ruby-trunk - Feature #4890] Enumerable#lazy Issue #4890 has been updated by Innokenty Mikhailov. Yusuke Endoh wrote: > > Yutaka, could you make a patch in C? Or anyone? > Hi, I've come up with patch in C - just two lazy methods added so far: map and select. Please, see this PR for more info https://github.com/ruby/ruby/pull/100 The idea is very simple - block that passed to lazy method (select or map) is converted to Proc and stored in enumerator itself. When next element requested - all Proc objects are called for this value and the result returned. Proc#call result handling depends on proc_entry type. Let me know if it makes any sense (if true - I can come up with additional lazy methods). Thanks. ---------------------------------------- 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/