my map vs. collect bone

T

Trans

I have a bone to pick. I tire of accounting for both method terms 'map'
and 'collect'. Anytime I create a method like #collect_with_counter I
feel obliged to create an alias #map_with_counter. And at this point my
collection of useful methods has a pretty good number of them. So my
feeling incerading become "This town's just not big enough for the both
of 'em". Besides its a waste of useful semanitic space.

Now when it comes to choosing between the two, I'm go for 'collect'
because its meaning is clearer to me even though its not as concise.
To me 'map' indicates 'hash' and its a strech to think of the resulting
array as a map. I suppose 'map' indicates that an input element
coorepsonds a resulting output element --but still that's weak. Lots of
things fit that description, like methods. I suspect it was really just
borrowed from lisp. Anyhow I digress.

The point is, can we just settle on one or the other for a future
version of Ruby? Although I prefer 'collect', I don't really care
which. Just pick one.

T.
 
D

Daniel Berger

Trans said:
I have a bone to pick. I tire of accounting for both method terms 'map'
and 'collect'. Anytime I create a method like #collect_with_counter I
feel obliged to create an alias #map_with_counter. And at this point my
collection of useful methods has a pretty good number of them. So my
feeling incerading become "This town's just not big enough for the both
of 'em". Besides its a waste of useful semanitic space.

That's a YP, not an MP. The same could be said of all aliases.
Now when it comes to choosing between the two, I'm go for 'collect'
because its meaning is clearer to me even though its not as concise.
To me 'map' indicates 'hash' and its a strech to think of the resulting
array as a map. I suppose 'map' indicates that an input element
coorepsonds a resulting output element --but still that's weak. Lots of
things fit that description, like methods. I suspect it was really just
borrowed from lisp. Anyhow I digress.

I prefer "map" for two reasons. First, I come from a Perl background,
which uses map, so it's familiar. Second, there's no "collecting" going
on. The most typical use for map is to alter each of the elements in
some way, i.e. to "map them to another value".
The point is, can we just settle on one or the other for a future
version of Ruby? Although I prefer 'collect', I don't really care
which. Just pick one.

T.

Extremely unlikely. Code breakage everywhere.

Regards,

Dan
 
B

Brian Schröder

I have a bone to pick. I tire of accounting for both method terms 'map'
and 'collect'. Anytime I create a method like #collect_with_counter I
feel obliged to create an alias #map_with_counter. And at this point my
collection of useful methods has a pretty good number of them. So my
feeling incerading become "This town's just not big enough for the both
of 'em". Besides its a waste of useful semanitic space.
=20
Now when it comes to choosing between the two, I'm go for 'collect'
because its meaning is clearer to me even though its not as concise.
To me 'map' indicates 'hash' and its a strech to think of the resulting
array as a map. I suppose 'map' indicates that an input element
coorepsonds a resulting output element --but still that's weak. Lots of
things fit that description, like methods. I suspect it was really just
borrowed from lisp. Anyhow I digress.
=20
The point is, can we just settle on one or the other for a future
version of Ruby? Although I prefer 'collect', I don't really care
which. Just pick one.
=20
T.
=20
=20
=20

I never use collect, because collect would seem more like select or
inject to me, while map makes it clear that the elements are mapped
onto other elements. (Not so clear in general maybe, but clear enough
to me ;)

regards,

Brian

--=20
http://ruby.brian-schroeder.de/

Stringed instrument chords: http://chordlist.brian-schroeder.de/
 
T

Trans

Daniel said:
Trans wrote:
That's a YP, not an MP. The same could be said of all aliases.

Perhaps. But this term particularly so b/c of it's frequent usage in
similar methods.
I prefer "map" for two reasons. First, I come from a Perl background,
which uses map, so it's familiar. Second, there's no "collecting" going
on.

Sure there is: the resulting values are returned in a _collection_ i.e.
an array.
The most typical use for map is to alter each of the elements in
some way, i.e. to "map them to another value".

Understandable reasoning. I think really it's largely a subjective
matter. So liek I said I don't care either way, I just wish there were
only one. It's not a big deal of course, it's just that it has crossed
my mind so many times now that I had to put the bone on the table :)
Extremely unlikely. Code breakage everywhere.

Thankfully we have the safely slow process of deprecation.

T.

P.S. I was checking out your latest blog post. Wanted to comment but
couldn't b/c I'm not OpenID'd. Does anyone know of any free OpenID
services? (I thought about signing up w/ LiveJournal but I alreay have
a blog.)

T.
 
P

Patrick Fernie

Regardless of the fact I like #map more than #collect, here's my
thought on maintaing both map_ and collect_: why not use a little
meta-programming? I'm not at my dev machine atm, so no ruby, but
generally:

module Enumerable
def Enumerable.method_added(meth)
if meth.to_s =3D~ /^collect_/
alias_method meth.to_s.sub(/^collect/, 'map').to_sym, meth
end
end
end

module Enumerable
def collect_test
puts "This is the new method"
end
end

a =3D []
a.collect_test #=3D> "This is the new method"
a.map_test #=3D> "This is the new method"

Or, you could use method_missing, which will help when defining
methods in classes including the module (the above case only works for
methods defined in the Enumerable module, I believe (away from Ruby
atm, so correct me if I'm wrong)):

module Enumerable
def method_missing(meth, *args)
if meth.to_s =3D~ /^map_/
self.send(meth.to_s.sub(/^map/, 'collect').to_sym, *args)
else
NoMethodError.new(meth.to_s)
end
end
end

class Array
def collect_another(str)
puts "Another: #{str}"
end
end

a =3D []
a.collect_another("yep!") #=3D> "Another: yep!"
a.map_another("also yep!") #=3D> "Another: also yep!"

I like this method less because then map_<whatever> doesn't show up in
documentation and when looking into the object. Also, it seems gragile
to me; somebody who knows more will probably correct me ;).

-P

Hi --
=20


I prefer #map because I do more Lisp than Smalltalk. :)
=20
I prefer to keep both because I do more Ruby than Lisp or Smalltalk
:)
=20
=20
David
=20[/QUOTE]
 
A

Adam Sanderson

I keep reading these ruby posts and smiling, I love this stuff. It
would be nice to start a collection of these sorts of rubycentric
solutions to problems. I know I'm still in a very rigid C++/Java
mindset when I'm writing code.
.adam
 

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

Forum statistics

Threads
473,754
Messages
2,569,522
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top