my map vs. collect bone

Discussion in 'Ruby' started by Trans, Aug 2, 2005.

  1. Trans

    Trans Guest

    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.
     
    Trans, Aug 2, 2005
    #1
    1. Advertising

  2. 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
     
    Daniel Berger, Aug 2, 2005
    #2
    1. Advertising

  3. On 02/08/05, 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.
    >=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/
     
    Brian Schröder, Aug 2, 2005
    #3
  4. Trans

    Trans Guest

    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.
     
    Trans, Aug 2, 2005
    #4
  5. Hi --

    On Wed, 3 Aug 2005, Christian Neukirchen wrote:

    > "Trans" <> 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
     
    David A. Black, Aug 2, 2005
    #5
  6. 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 <> wrote:
    > Hi --
    >=20
    > On Wed, 3 Aug 2005, Christian Neukirchen wrote:
    >=20
    > > "Trans" <> 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
    >
    >
     
    Patrick Fernie, Aug 2, 2005
    #6
  7. Trans

    Trans Guest

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

    T.
     
    Trans, Aug 2, 2005
    #7
  8. 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
     
    Adam Sanderson, Aug 3, 2005
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. HDL Book Seller
    Replies:
    0
    Views:
    458
    HDL Book Seller
    Aug 6, 2004
  2. Zoran Lazarevic
    Replies:
    5
    Views:
    199
    Ara.T.Howard
    Oct 8, 2003
  3. Gary Watson

    Oppinions on map vs collect

    Gary Watson, Dec 5, 2005, in forum: Ruby
    Replies:
    7
    Views:
    163
    Daniel Schierbeck
    Dec 5, 2005
  4. Kelly Dwight Felkins
    Replies:
    6
    Views:
    155
    Jeremy Henty
    Feb 9, 2006
  5. Dark Ambient
    Replies:
    10
    Views:
    204
    Dark Ambient
    Aug 13, 2006
Loading...

Share This Page