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 B. IV
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 02/04/07, Nicholas M. [email protected] 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
On 02.04.2007 15:17, Nicholas M. 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
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 use map, but then I was a Mathematics major, which may have something
to do with it …
On 4/2/07, [email protected] [email protected] 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
[email protected] 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
On 4/2/07, [email protected] [email protected] 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
On 4/2/07, [email protected] [email protected] wrote:
What does Matz use?
Stephen B. IV
WWMD?