Project

General

Profile

Actions

Feature #707

closed

Documentation for Enumerator chaining

Feature #707: Documentation for Enumerator chaining

Added by candlerb (Brian Candler) almost 17 years ago. Updated over 13 years ago.

Status:
Closed
Target version:
[ruby-core:19679]

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


Files

fib.rb (676 Bytes) fib.rb candlerb (Brian Candler), 11/03/2008 06:41 PM
enumerator.c.lazy.patch (2.7 KB) enumerator.c.lazy.patch drbrain (Eric Hodel), 02/11/2012 11:01 AM

Updated by candlerb (Brian Candler) almost 17 years ago Actions #1

=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

Updated by ko1 (Koichi Sasada) almost 17 years ago Actions #2

  • Assignee set to matz (Yukihiro Matsumoto)

=begin

=end

Updated by rogerdpack (Roger Pack) almost 16 years ago Actions #3

=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

Updated by candlerb (Brian Candler) almost 16 years ago Actions #4

=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

Updated by shyouhei (Shyouhei Urabe) about 15 years ago Actions #5

  • Status changed from Open to Assigned

=begin

=end

Updated by mame (Yusuke Endoh) over 13 years ago Actions #6 [ruby-core:42401]

  • Assignee changed from matz (Yukihiro Matsumoto) to drbrain (Eric Hodel)

Drbrain, could you take over this documentation ticket?

--
Yusuke Endoh

Updated by drbrain (Eric Hodel) over 13 years ago Actions #7 [ruby-core:42433]

  • Priority changed from 3 to Normal

I will work on it, thank you.

Updated by drbrain (Eric Hodel) over 13 years ago Actions #8 [ruby-core:42498]

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).

Updated by mame (Yusuke Endoh) over 13 years ago Actions #9 [ruby-core:42526]

Look good to me. Please go ahead!

--
Yusuke Endoh

Updated by drbrain (Eric Hodel) over 13 years ago Actions #10

  • 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]
Actions

Also available in: PDF Atom