Project

General

Profile

Actions

Feature #707

closed

Documentation for Enumerator chaining

Added by candlerb (Brian Candler) about 16 years ago. Updated almost 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
Actions #1

Updated by candlerb (Brian Candler) about 16 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

Actions #2

Updated by ko1 (Koichi Sasada) almost 16 years ago

  • Assignee set to matz (Yukihiro Matsumoto)

=begin

=end

Actions #3

Updated by rogerdpack (Roger Pack) about 15 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

Actions #4

Updated by candlerb (Brian Candler) about 15 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

Actions #5

Updated by shyouhei (Shyouhei Urabe) about 14 years ago

  • Status changed from Open to Assigned

=begin

=end

Updated by mame (Yusuke Endoh) almost 13 years ago

  • 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) almost 13 years ago

  • Priority changed from 3 to Normal

I will work on it, thank you.

Updated by drbrain (Eric Hodel) almost 13 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).

Updated by mame (Yusuke Endoh) almost 13 years ago

Look good to me. Please go ahead!

--
Yusuke Endoh

Actions #10

Updated by drbrain (Eric Hodel) almost 13 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]
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0