Feature #709

Enumerator#+

Added by Brian Candler over 6 years ago. Updated almost 4 years ago.

[ruby-core:19681]
Status:Rejected
Priority:Low
Assignee:Akinori MUSHA

Description

=begin
Enumerators could be directly composable:

class Enumerator
def +(other)
Enumerator.new do |y|
each { |e| y << e }
other.each { |e| y << e }
end
end
end

if FILE == $0
a = (1..3).to_enum + (10..12).to_enum + (17..20).to_enum
a.each { |i| puts i }
end

The only problem I can see here is that this might open the floodgates to requests for more methods such as & and |, and it's not clear how those would behave. (Personally I'd go for a merge which compares the head items from each of the operands, which assumes that the operands are already in sorted order, but others may disagree)
=end

History

#1 Updated by Brian Candler over 6 years ago

=begin
For clarity, here's what that example outputs:

$ ruby19 -v
ruby 1.9.1 (2008-10-28 revision 19983) [i686-linux]
$ ruby19 enum_plus.rb
1
2
3
10
11
12
17
18
19
20
$

=end

#2 Updated by Koichi Sasada over 6 years ago

  • Assignee set to Yukihiro Matsumoto

=begin

=end

#3 Updated by Akinori MUSHA over 6 years ago

  • Status changed from Open to Rejected
  • Assignee changed from Yukihiro Matsumoto to Akinori MUSHA

=begin
To tell the truth, I was also tempted to add something like that and
you are not alone.

You might as well add:

class Enumerator
alias * cycle

def -@
Enumerator.new { |y| to_a.reverse_each { |i| y << i } }
end
end

and have fun with them as follows:

e = -[1,4,7].each * 3 + ("a".."c").each
p e.to_a #=> [3, 2, 1, 3, 2, 1, 3, 2, 1, "a", "b", "c"]

However, I am yet to think that even the addition of enumerators is so
commonly in need.

Besides, as you said, operators must be carefully chosen and defined so
that they work consistently and systematically and they look intuitive
and practical, all at the same time. We should also think how other
types of iterators should be introduced, such as bidirectional iterators
and indexable iterators, before defining a variety of operators for
the current Enumerator.

So, my answer here, for now, is the block based enumerator. Just as you
have aptly shown, you can define anything you like with it without the
need for predefined operators.
=end

Also available in: Atom PDF