My map vs. collect bone

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.

Trans wrote:

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

"Trans" <transfire@gmail.com> writes:

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.

I prefer #map because I do more Lisp than Smalltalk. :slight_smile:

···

T.

--
Christian Neukirchen <chneukirchen@gmail.com> http://chneukirchen.org

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 :wink:

regards,

Brian

···

On 02/08/05, Trans <transfire@gmail.com> wrote:

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.

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

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

Hi --

···

On Wed, 3 Aug 2005, Christian Neukirchen wrote:

"Trans" <transfire@gmail.com> writes:

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.

I prefer #map because I do more Lisp than Smalltalk. :slight_smile:

I prefer to keep both because I do more Ruby than Lisp or Smalltalk
:slight_smile:

David

--
David A. Black
dblack@wobblini.net

Daniel Berger wrote:

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 :slight_smile:

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.

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 =~ /^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 =
a.collect_test #=> "This is the new method"
a.map_test #=> "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 =~ /^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 =
a.collect_another("yep!") #=> "Another: yep!"
a.map_another("also yep!") #=> "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

···

On 8/2/05, David A. Black <dblack@wobblini.net> wrote:

Hi --

On Wed, 3 Aug 2005, Christian Neukirchen wrote:

> "Trans" <transfire@gmail.com> writes:
>
>> 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.
>
> I prefer #map because I do more Lisp than Smalltalk. :slight_smile:

I prefer to keep both because I do more Ruby than Lisp or Smalltalk
:slight_smile:

David

--
David A. Black
dblack@wobblini.net

He he. It's amazing what Ruby can do, ain't it Patrick?

T.

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