Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > my map vs. collect bone

Reply
Thread Tools

my map vs. collect bone

 
 
Trans
Guest
Posts: n/a
 
      08-02-2005
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.

 
Reply With Quote
 
 
 
 
Daniel Berger
Guest
Posts: n/a
 
      08-02-2005
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



 
Reply With Quote
 
 
 
 
Brian Schröder
Guest
Posts: n/a
 
      08-02-2005
On 02/08/05, Trans <(E-Mail Removed)> 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.
>=20
> 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.
>=20
> 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.
>=20
> T.
>=20
>=20
>=20


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

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

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


 
Reply With Quote
 
Trans
Guest
Posts: n/a
 
      08-02-2005

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.

 
Reply With Quote
 
David A. Black
Guest
Posts: n/a
 
      08-02-2005
Hi --

On Wed, 3 Aug 2005, Christian Neukirchen wrote:

> "Trans" <(E-Mail Removed)> 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
http://www.velocityreviews.com/forums/(E-Mail Removed)


 
Reply With Quote
 
Patrick Fernie
Guest
Posts: n/a
 
      08-02-2005
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 =3D~ /^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 =3D []
a.collect_test #=3D> "This is the new method"
a.map_test #=3D> "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 =3D~ /^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 =3D []
a.collect_another("yep!") #=3D> "Another: yep!"
a.map_another("also yep!") #=3D> "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 <(E-Mail Removed)> wrote:
> Hi --
>=20
> On Wed, 3 Aug 2005, Christian Neukirchen wrote:
>=20
> > "Trans" <(E-Mail Removed)> 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.

>=20
> I prefer to keep both because I do more Ruby than Lisp or Smalltalk
>
>=20
>=20
> David
>=20
> --
> David A. Black
> (E-Mail Removed)
>



 
Reply With Quote
 
Trans
Guest
Posts: n/a
 
      08-02-2005

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

T.

 
Reply With Quote
 
Adam Sanderson
Guest
Posts: n/a
 
      08-03-2005
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
snn5-fw.n-bone.net sam Computer Security 0 06-23-2006 06:47 AM
"Bone Sickness" RichA DVD Video 0 04-20-2005 08:17 PM
WEb Designer thinking of getting ontothe Back Bone peter NZ Computing 13 09-21-2004 12:44 AM
Bone up on VHDL & Verilog with these great reference texts at 60% off Amazon HDL Book Seller VHDL 0 08-06-2004 05:00 PM



Advertisments