Skip to content

enumerator extended with lazy select and map #100

wants to merge 3 commits into from

2 participants


Please, see for more info about enumerator laziness.

I've added Enumerator#lazy method that marks enumerator as lazy.
Also lazy Enumerator#map and Enumerator#select defined.
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.


a = [1,2,3,4].to_enum.lazy { |a| a*10 }.select { |a| a > 10 }
puts #=> 20
puts #=> 30
puts #=> 40

Keep in mind - I'm kinda a newbie in ruby patching, so please, let me know your thoughts/fixes/comments.
Does it make any sense to add laziness this way?


trans commented Feb 24, 2012

Is this marking the Enumerator in place rather returning a new "lazy enumerator"?


Yeah, it's marking in place. But that's not a problem since we have enumerator_init_copy, that can be used in Enumerator#lazy.
This PR is to demonstrate an idea, if the idea makes sense - I'll add necessary fixes and methods.

@gregolsen gregolsen closed this Mar 5, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.