RCR enumerable extra into core

D

David A. Black

Hi --

The OP was asking whether map:)foo) should return an Array or an
Enumerator. I was trying to say that if you're ambivalent about this,
one logical conclusion (or extreme viewpoint) is that you could always
return an Enumerator, even for

map { |x| x*x }

I wasn't saying that Ruby does anything like this, and it's a tangent to
the original thrust of map:)foo).

This reminds me of the early 1.9.0 thing, where you could do:

array = [1,2,3,4]
enum = array.enum_for:)map, &lambda {|x| x * x })

enum.next # 1
enum.next # 4

etc. (That's ruby 1.9.0 (2008-03-01 revision 15660)
[i686-darwin9.2.0].) To be honest, when that disappeared, it seemed to
me to do away with a great deal of the usefulness of enumerators.
Given that you can no longer attach a block to an enumerator when you
create the enumerator, I don't think there are any actual use cases
for the fact that map returns an enumerator (or at least very, very
few). You can't do this, for example (following the above example):

enum.select {|x| x > 1 } # [4, 9, 16]


David

--
The Ruby training with D. Black, G. Brown, J.McAnally
Compleat Jan 22-23, 2010, Tampa, FL
Rubyist http://www.thecompleatrubyist.com

David A. Black/Ruby Power and Light, LLC (http://www.rubypal.com)
 
B

Benoit Daloze

[Note: parts of this message were removed to make it a legal post.]

Hi,

I'm quite conservative or maybe 'wise', seeing this discussion.

In fact, using arr.map:)method, arg1 ... ) is too confusing to me
While the &:method notation is clear, and not that ugly.

And I think it has to be a separation between blocks and arguments; block is
another "kind" of argument.
That's why I think we should keep the &:method, and forget about :method.
Note also that is not a common use of &:method, because you often need to do
ore than one thing with the element. eg:
[1,15,3].map { |e| e.to_s.length }

P.S.:
Why &:method is faster ? (Bad, I wanted to argue &:method is slower but I
can't ...)
(requiring and including Benchmark)
irb(main):008:0> realtime { (1..1000000).to_a.map &:to_s }
=> 0.38111090660095215
irb(main):009:0> realtime { (1..1000000).to_a.map { |e| e.to_s } }
=> 0.445728063583374


2009/11/11 David A. Black said:
Hi --


The OP was asking whether map:)foo) should return an Array or an
Enumerator. I was trying to say that if you're ambivalent about this,
one logical conclusion (or extreme viewpoint) is that you could always
return an Enumerator, even for

map { |x| x*x }

I wasn't saying that Ruby does anything like this, and it's a tangent to
the original thrust of map:)foo).

This reminds me of the early 1.9.0 thing, where you could do:

array = [1,2,3,4]
enum = array.enum_for:)map, &lambda {|x| x * x })

enum.next # 1
enum.next # 4

etc. (That's ruby 1.9.0 (2008-03-01 revision 15660)
[i686-darwin9.2.0].) To be honest, when that disappeared, it seemed to
me to do away with a great deal of the usefulness of enumerators.
Given that you can no longer attach a block to an enumerator when you
create the enumerator, I don't think there are any actual use cases
for the fact that map returns an enumerator (or at least very, very
few). You can't do this, for example (following the above example):

enum.select {|x| x > 1 } # [4, 9, 16]



David

--
The Ruby training with D. Black, G. Brown, J.McAnally
Compleat Jan 22-23, 2010, Tampa, FL
Rubyist http://www.thecompleatrubyist.com

David A. Black/Ruby Power and Light, LLC (http://www.rubypal.com)
 
R

Roger Pack

This reminds me of the early 1.9.0 thing, where you could do:
array = [1,2,3,4]
enum = array.enum_for:)map, &lambda {|x| x * x })

enum.next # 1
enum.next # 4

It is surprising that currently it appears to *ignore* the given block.
=> "xx\n"

Is this expected?
-r
 
R

Roger Pack

This reminds me of the early 1.9.0 thing, where you could do:
array = [1,2,3,4]
enum = array.enum_for:)map, &lambda {|x| x * x })

enum.next # 1
enum.next # 4


Appears somewhat possible...

class Enumerator
def filter(&blk)
self.class.new do |y|
each do |*input|
out = blk.call(*input)
y << out
# or y << out if out
end
end
end
end
array = [1,2,3,4]
enum = array.enum_for:)map).filter {|x| x * x }
enum.next => 1
enum.next
=> 4
enum.select {|x| x > 1 } # [4, 9, 16]
=> [4, 9, 16]

[Thanks to Brian Candler for the snippet--though perhaps it could be
improved...]

This should be the default, though--I don't know why blocks are ignored
currently...

-r

http://www.ruby-forum.com/topic/169844#new
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,164
Messages
2,570,898
Members
47,439
Latest member
shasuze

Latest Threads

Top