Feature #707

Documentation for Enumerator chaining

Added by Brian Candler over 6 years ago. Updated about 3 years ago.

[ruby-core:19679]
Status:Closed
Priority:Normal
Assignee:Eric Hodel

Description

=begin
Enumerators now support a horizontal filter/chaining pattern:

generator.filter1 { ... }.filter2 { ... }.filter3 { ... }.consumer

The overall pattern for a filter is:

Enumerator.new do |y|
source.each do |input| # filter INPUT
...
y << output # filter OUTPUT
end
end

This is extremely powerful. However it is not obvious to the newcomer that this is even possible. (Confusion may arise where people know Ruby 1.8's Enumerator, which cannot do this)

So I would just like to see this pattern documented with an example, e.g. under ri Enumerator.new

I have attached a possible example. Note that I have written my Fib generator in a style familiar from ruby-1.8, to emphasise that the Enumerator filter doesn't require a specially-written generator.
=end

fib.rb Magnifier (676 Bytes) Brian Candler, 11/03/2008 06:41 PM

enumerator.c.lazy.patch Magnifier (2.7 KB) Eric Hodel, 02/11/2012 11:01 AM

Associated revisions

Revision 34586
Added by Eric Hodel about 3 years ago

  • enumerator.c: Document use of Enumerator.new for creating a lazy enumeration for filtering/chaining. [ruby-trunk - Feature #707]

Revision 34586
Added by Eric Hodel about 3 years ago

  • enumerator.c: Document use of Enumerator.new for creating a lazy enumeration for filtering/chaining. [ruby-trunk - Feature #707]

History

#1 Updated by Brian Candler over 6 years ago

=begin
Here is another (shorter and simpler)

class Enumerator
def filter(&blk)
self.class.new do |y|
each do |*input|
blk.call(y, *input)
end
end
end
end

a = (1..1_000_000_000).to_enum
a.filter { |out,inp| out << inp if inp % 2 == 0 }.
filter { |out,inp| out << inp+100 }.
with_index.each { |inp,c| puts inp; break if c > 10 }

=end

#2 Updated by Koichi Sasada over 6 years ago

  • Assignee set to Yukihiro Matsumoto

=begin

=end

#3 Updated by Roger Pack over 5 years ago

=begin
Is this also possible in 1.8?

http://www.ruby-forum.com/topic/198804#new
http://github.com/trans/facets/blob/master/lib/core/facets/denumerable.rb

Brian do you think you could write the tutorial on chaining started here:
http://wiki.github.com/rdp/ruby_tutorials_core/enumerator
?
-r
=end

#4 Updated by Brian Candler over 5 years ago

=begin

Is this also possible in 1.8?

Sure. Block-form Enumerator is surprisingly straightforward to implement, see
http://github.com/trans/facets/blob/master/lib/more/facets/enumerator.rb

But since 1.9 has it already, I think it's worth documenting the possibilities more clearly.
=end

#5 Updated by Shyouhei Urabe over 4 years ago

  • Status changed from Open to Assigned

=begin

=end

#6 Updated by Yusuke Endoh about 3 years ago

  • Assignee changed from Yukihiro Matsumoto to Eric Hodel

Drbrain, could you take over this documentation ticket?

Yusuke Endoh mame@tsg.ne.jp

#7 Updated by Eric Hodel about 3 years ago

  • Priority changed from Low to Normal

I will work on it, thank you.

#8 Updated by Eric Hodel about 3 years ago

What do you think about this patch?

I chose not to monkey-patch Enumerator since I don't want the documentation to collide with #4890

Also, I think assigning the lazy enumerators helps illustrate the difference between lazy_select and select (select would never return).

#9 Updated by Yusuke Endoh about 3 years ago

Look good to me. Please go ahead!

Yusuke Endoh mame@tsg.ne.jp

#10 Updated by Eric Hodel about 3 years ago

  • Status changed from Assigned to Closed
  • % Done changed from 0 to 100

This issue was solved with changeset r34586.
Brian, thank you for reporting this issue.
Your contribution to Ruby is greatly appreciated.
May Ruby be with you.


  • enumerator.c: Document use of Enumerator.new for creating a lazy enumeration for filtering/chaining. [ruby-trunk - Feature #707]

Also available in: Atom PDF