[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Map Or Collect Redux

RubyTalk@gmail.com

4/2/2007 1:12:00 PM

Looking in the old archives of ruby-talk I found a thread in 2005
about using map or collect. As far as I know there is not difference
between the two methods. I would like to know what everyone's
preference is?

I like using collect.


Stephen Becker IV

11 Answers

Nicholas Milkovits

4/2/2007 1:17:00 PM

0

I think the aliases are there for a good syntactic reason. It seems
to me that if you are just filtering out items which meet a certain
criteria from one array to be put into a new one (say all elements
which are even numbers) then you are collecting. If you're actually
performing a transformation on the elements then you are mapping one
array space onto another.

example

arr = [1, 2, 3, 4, 5]
arr.collect { |x| if x%2 == 0 then x end }.compact
=> [2, 4]
# Seems like maybe it should be .filter instead of .collect

arr = [1, 2, 3, 4, 5]
arr.map { |x| x*2 }
=> [2, 4, 6, 8, 10]

On 4/2/07, RubyTalk@gmail.com <rubytalk@gmail.com> wrote:
> Looking in the old archives of ruby-talk I found a thread in 2005
> about using map or collect. As far as I know there is not difference
> between the two methods. I would like to know what everyone's
> preference is?
>
> I like using collect.
>
>
> Stephen Becker IV
>
>

Robert Klemme

4/2/2007 1:21:00 PM

0

On 02.04.2007 15:17, Nicholas Milkovits wrote:
> I think the aliases are there for a good syntactic reason. It seems
> to me that if you are just filtering out items which meet a certain
> criteria from one array to be put into a new one (say all elements
> which are even numbers) then you are collecting.

No, you are actually *selecting*.

> If you're actually
> performing a transformation on the elements then you are mapping one
> array space onto another.

#map and #collect behave identical. They are just there to make people
coming from different programming languages feel at home with Ruby faster.

> example
>
> arr = [1, 2, 3, 4, 5]
> arr.collect { |x| if x%2 == 0 then x end }.compact
> => [2, 4]
> # Seems like maybe it should be .filter instead of .collect

No, use #select - much more efficient and it does exactly what you are
trying to accomplish above.

Regards

robert


PS: for the record, I use #map as it is shorter to type and I am laz

Farrel Lifson

4/2/2007 1:24:00 PM

0

On 02/04/07, Nicholas Milkovits <nmilkovits@gmail.com> wrote:
> arr = [1, 2, 3, 4, 5]
> arr.collect { |x| if x%2 == 0 then x end }.compact
> => [2, 4]
> # Seems like maybe it should be .filter instead of .collect

This is more appropriate for Array#select (or reject).

Personally I use map exclusively.

Farrel

Nicholas Milkovits

4/2/2007 2:00:00 PM

0

Enumerable#select is certainly what I was thinking of. Certainly
having both collect and map would make it easier for developers coming
from other languages to pick up ruby but often times I see code where
these methods are both used. Ruby reads very nice and I prefer to use
map just because it semantically makes more sense to me if I am
performing some sort of transformation on the array elements. (Plus
it is shorter to type : ) ). When I think of collecting elements I
think of gathering or *selecting* certain elements, collect just does
not seem to imply that the elements will be changed in any way to me.

On 4/2/07, Robert Klemme <shortcutter@googlemail.com> wrote:
> On 02.04.2007 15:17, Nicholas Milkovits wrote:
> > I think the aliases are there for a good syntactic reason. It seems
> > to me that if you are just filtering out items which meet a certain
> > criteria from one array to be put into a new one (say all elements
> > which are even numbers) then you are collecting.
>
> No, you are actually *selecting*.
>
> > If you're actually
> > performing a transformation on the elements then you are mapping one
> > array space onto another.
>
> #map and #collect behave identical. They are just there to make people
> coming from different programming languages feel at home with Ruby faster.
>
> > example
> >
> > arr = [1, 2, 3, 4, 5]
> > arr.collect { |x| if x%2 == 0 then x end }.compact
> > => [2, 4]
> > # Seems like maybe it should be .filter instead of .collect
>
> No, use #select - much more efficient and it does exactly what you are
> trying to accomplish above.
>
> Regards
>
> robert
>
>
> PS: for the record, I use #map as it is shorter to type and I am laz
>
>

Ryan Leavengood

4/2/2007 2:44:00 PM

0

On 4/2/07, RubyTalk@gmail.com <rubytalk@gmail.com> wrote:
> Looking in the old archives of ruby-talk I found a thread in 2005
> about using map or collect. As far as I know there is not difference
> between the two methods. I would like to know what everyone's
> preference is?

I use map. It is shorter and for me it describes the operation better.

Ryan

Chris Gernon

4/2/2007 3:13:00 PM

0

I use map, but then I was a Mathematics major, which may have something
to do with it ... ;)

--
Posted via http://www.ruby-....

RubyTalk@gmail.com

4/2/2007 4:49:00 PM

0

What does Matz use?

Stephen Becker IV

On 4/2/07, Chris Gernon <kabigon@gmail.com> wrote:
> I use map, but then I was a Mathematics major, which may have something
> to do with it ... ;)
>
> --
> Posted via http://www.ruby-....
>
>

Rob Sanheim

4/2/2007 5:14:00 PM

0

On 4/2/07, RubyTalk@gmail.com <rubytalk@gmail.com> wrote:
> What does Matz use?
>
> Stephen Becker IV

WWMD?

Devin Mullins

4/2/2007 9:07:00 PM

0

RubyTalk@gmail.com wrote:
> I would like to know what everyone's preference is?
> I like using collect.

Mostly the non-smalltalk terms: map, find, find_all, but reject. (map ==
mathematics background.)

When I'm dealing with an ActiveRecord::AssociationProxy, find and
find_all are overridden by Rails, so I tend to use detect and select.
I'm not sure I like that, though, vice just saying to_a.find.

Devin

Martin DeMello

4/2/2007 9:33:00 PM

0

On 4/2/07, RubyTalk@gmail.com <rubytalk@gmail.com> wrote:
> Looking in the old archives of ruby-talk I found a thread in 2005
> about using map or collect. As far as I know there is not difference
> between the two methods. I would like to know what everyone's
> preference is?

I use map for the most part - the one place I occasionally use collect
is when I'm mapping over a struct or class to pick out a few fields,
e.g.

books.collect {|b| [b.author, b.title]}

If I were designing my own language, I'd have map be
structure-preserving, and collect return an array, but that's just my
personal feeling.

martin