Skip to content

enumerator extended with lazy select and map #100

Closed
wants to merge 3 commits into from

2 participants

@gregolsen

Please, see http://bugs.ruby-lang.org/issues/4890 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.

Example:

a = [1,2,3,4].to_enum.lazy
a.map { |a| a*10 }.select { |a| a > 10 }
puts a.next #=> 20
puts a.next #=> 30
puts a.next #=> 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?

Thanks.

@trans
trans commented Feb 24, 2012

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

@gregolsen

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.