Map Or Collect Redux

R

RubyTalk

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
 
N

Nicholas Milkovits

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]
 
R

Robert Klemme

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
 
F

Farrel Lifson

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
 
N

Nicholas Milkovits

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.

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
 
R

Ryan Leavengood

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
 
C

Chris Gernon

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

Devin Mullins

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
 
M

Martin DeMello

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
 

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
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top