# My map vs. collect bone

**URL:** <https://rubytalk.org/t/my-map-vs-collect-bone/19898>\
**Category:** ruby-talk\
**Created:** [2 August 2005 15:06 UTC](https://rubytalk.org/t/my-map-vs-collect-bone/19898 "2005-08-02T15:06:04Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![7rans](https://avatars.discourse-cdn.com/v4/letter/7/a8b319/32.png) [@7rans](https://rubytalk.org/u/7rans)\
**Post date:** [2 August 2005 15:06 UTC](https://rubytalk.org/t/my-map-vs-collect-bone/19898/1 "2005-08-02T15:06:04Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Berger\_Daniel1](https://avatars.discourse-cdn.com/v4/letter/b/ecc23a/32.png) [@Berger\_Daniel1](https://rubytalk.org/u/Berger_Daniel1)\
**Post date:** [2 August 2005 15:11 UTC](https://rubytalk.org/t/my-map-vs-collect-bone/19898/2 "2005-08-02T15:11:40Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Christian\_Neukirche1](https://avatars.discourse-cdn.com/v4/letter/c/ee7513/32.png) [@Christian\_Neukirche1](https://rubytalk.org/u/Christian_Neukirche1)\
**Post date:** [2 August 2005 15:41 UTC](https://rubytalk.org/t/my-map-vs-collect-bone/19898/3 "2005-08-02T15:41:09Z")

</div>

"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. 🙂

> **···**
>
> > T.
> 
> --  
> Christian Neukirchen \<chneukirchen@gmail.com\> [http://chneukirchen.org](http://chneukirchen.org)

---

<div class="post-metadata">

**Author:** ![Brian\_Schroder1](https://avatars.discourse-cdn.com/v4/letter/b/2bfe46/32.png) [@Brian\_Schroder1](https://rubytalk.org/u/Brian_Schroder1)\
**Post date:** [2 August 2005 16:12 UTC](https://rubytalk.org/t/my-map-vs-collect-bone/19898/4 "2005-08-02T16:12:55Z")

</div>

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

> **···**
>
> 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/](http://ruby.brian-schroeder.de/)
> 
> Stringed instrument chords: [http://chordlist.brian-schroeder.de/](http://chordlist.brian-schroeder.de/)

---

<div class="post-metadata">

**Author:** ![David\_A\_Black3](https://avatars.discourse-cdn.com/v4/letter/d/6a8cbe/32.png) [@David\_A\_Black3](https://rubytalk.org/u/David_A_Black3)\
**Post date:** [2 August 2005 16:19 UTC](https://rubytalk.org/t/my-map-vs-collect-bone/19898/5 "2005-08-02T16:19:46Z")

</div>

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. 🙂
> 
> I prefer to keep both because I do more Ruby than Lisp or Smalltalk  
> 🙂
> 
> David
> 
> --  
> David A. Black  
> dblack@wobblini.net

---

<div class="post-metadata">

**Author:** ![7rans](https://avatars.discourse-cdn.com/v4/letter/7/a8b319/32.png) [@7rans](https://rubytalk.org/u/7rans)\
**Post date:** [2 August 2005 16:21 UTC](https://rubytalk.org/t/my-map-vs-collect-bone/19898/6 "2005-08-02T16:21:06Z")

</div>

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 🙂

> 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.

---

<div class="post-metadata">

**Author:** ![Patrick\_Fernie](https://avatars.discourse-cdn.com/v4/letter/p/65b543/32.png) [@Patrick\_Fernie](https://rubytalk.org/u/Patrick_Fernie)\
**Post date:** [2 August 2005 16:40 UTC](https://rubytalk.org/t/my-map-vs-collect-bone/19898/7 "2005-08-02T16:40:08Z")

</div>

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  
&nbsp;&nbsp;def Enumerable.method\_added(meth)  
&nbsp;&nbsp;&nbsp;&nbsp;if meth.to\_s =~ /^collect\_/  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;alias\_method meth.to\_s.sub(/^collect/, 'map').to\_sym, meth  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;end  
end

module Enumerable  
&nbsp;&nbsp;def collect\_test  
&nbsp;&nbsp;&nbsp;&nbsp;puts "This is the new method"  
&nbsp;&nbsp;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  
&nbsp;&nbsp;def method\_missing(meth, \*args)  
&nbsp;&nbsp;&nbsp;&nbsp;if meth.to\_s =~ /^map\_/  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.send(meth.to\_s.sub(/^map/, 'collect').to\_sym, \*args)  
&nbsp;&nbsp;&nbsp;&nbsp;else  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;NoMethodError.new(meth.to\_s)  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;end  
end

class Array  
&nbsp;&nbsp;def collect\_another(str)  
&nbsp;&nbsp;&nbsp;&nbsp;puts "Another: #{str}"  
&nbsp;&nbsp;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. 🙂
> > 
> > I prefer to keep both because I do more Ruby than Lisp or Smalltalk  
> > 🙂
> > 
> > David
> > 
> > --  
> > David A. Black  
> > dblack@wobblini.net

---

<div class="post-metadata">

**Author:** ![7rans](https://avatars.discourse-cdn.com/v4/letter/7/a8b319/32.png) [@7rans](https://rubytalk.org/u/7rans)\
**Post date:** [2 August 2005 20:21 UTC](https://rubytalk.org/t/my-map-vs-collect-bone/19898/8 "2005-08-02T20:21:04Z")

</div>

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

T.

---

<div class="post-metadata">

**Author:** ![Adam\_Sanderson](https://avatars.discourse-cdn.com/v4/letter/a/2acd7d/32.png) [@Adam\_Sanderson](https://rubytalk.org/u/Adam_Sanderson)\
**Post date:** [3 August 2005 17:06 UTC](https://rubytalk.org/t/my-map-vs-collect-bone/19898/9 "2005-08-03T17:06:06Z")

</div>

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.  
&nbsp;&nbsp;.adam
