Naming question

Discussion in 'Ruby' started by Robert Dober, Apr 18, 2008.

  1. Robert Dober

    Robert Dober Guest

    Hi list

    I actually just need the following method there is no doubt about it;)
    but the name which sprang into mind naturally "partition_by"
    has a serious shortcoming, it is not POLS in the sense that
    Enumerable#partition returns an array and Enumerable#partition_by
    returns a hash.

    module Enumerable
    def partition_by &blk
    result = Hash.new {|h,k| h[k] = [] }
    each do | element |
    result[ element ] << blk.call element
    end
    result
    end
    end


    Any ideas for a nice different name?
    Does Facet implement the above maybe?

    A remark for my fellow fans of inject, the same code with inject
    somehow does not feel right, I do not like to use inject with this
    pattern
    inject(...){ |acc,ele|
    ...
    acc
    }

    Thanx in advance
    Robert

    --
    http://ruby-smalltalk.blogspot.com/

    ---
    Whereof one cannot speak, thereof one must be silent.
    Ludwig Wittgenstein
    Robert Dober, Apr 18, 2008
    #1
    1. Advertising

  2. Robert Dober

    Robert Dober Guest

    On Fri, Apr 18, 2008 at 9:36 AM, Robert Dober <> wrote:
    >


    > Does Facet implement the above maybe?

    Enumerable#cluster_by
    great that would have been a nice name thanx Tom ;)
    but I can live with your group_by :)
    issue resolved

    Actually I never thaught that Facets doc was so darn pretty good that
    I could find it myself, should never have asked....

    R.
    --
    http://ruby-smalltalk.blogspot.com/

    ---
    Whereof one cannot speak, thereof one must be silent.
    Ludwig Wittgenstein
    Robert Dober, Apr 18, 2008
    #2
    1. Advertising

  3. Robert Dober

    Peña, Botp Guest

    From: Robert Dober [mailto:]=20
    # module Enumerable
    # def partition_by &blk
    # result =3D Hash.new {|h,k| h[k] =3D [] }
    # each do | element |
    # result[ element ] << blk.call element
    # end
    # result
    # end
    # end

    i think this is #group_by in 1.8.7 and 1.9 ?

    kind regards -botp
    Peña, Botp, Apr 18, 2008
    #3
  4. On Fri, Apr 18, 2008 at 4:02 AM, Pe=F1a, Botp <> wrot=
    e:
    > From: Robert Dober [mailto:]
    > # module Enumerable
    >
    > # def partition_by &blk
    > # result =3D Hash.new {|h,k| h[k] =3D [] }
    > # each do | element |
    > # result[ element ] << blk.call element
    > # end
    > # result
    > # end
    > # end
    >
    > i think this is #group_by in 1.8.7 and 1.9 ?


    Which, I believe, first showed up in Rails sometime in 2006.

    --=20
    Rick DeNatale

    My blog on Ruby
    http://talklikeaduck.denhaven2.com/
    Rick DeNatale, Apr 18, 2008
    #4
  5. On Fri, Apr 18, 2008 at 12:10 PM, Rick DeNatale <> wrote:
    >
    > Which, I believe, first showed up in Rails sometime in 2006.
    >
    > --
    > Rick DeNatale
    >


    Florian Gross published a version in Jan 2005:
    http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-core/4311

    There are possibly even earlier ones.

    Credit where credit is due :)

    Regards,
    Sean
    Sean O'Halpin, Apr 18, 2008
    #5
  6. Robert Dober

    Robert Dober Guest

    On Fri, Apr 18, 2008 at 6:04 PM, Sean O'Halpin <> wrote:
    > On Fri, Apr 18, 2008 at 12:10 PM, Rick DeNatale <> wrote:
    > >
    > > Which, I believe, first showed up in Rails sometime in 2006.
    > >
    > > --
    > > Rick DeNatale
    > >

    >
    > Florian Gross published a version in Jan 2005:
    > http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-core/4311

    module Enumerable
    def group_by
    result = Hash.new { |h, k| h[k] = Array.new }
    self.each do |item|
    group = yield(item)
    result[group] << item
    end
    return result
    end
    end

    This is the code of the post linked to above, as one can see it works,
    not as mine above :)
    but this natural way to write it is quite inefficient as the Facets
    version shows. I have benchmarked them and to my surprise and that of
    many others I guess the Facets implementation is almost twice as fast
    - for many different keys at least

    result = {}
    each do | ele|
    ( result[ yield(ele) ] ||= [] ) << ele
    end
    result.

    Does anybody have the faintest idea why Hash.new{|h,k| ... } is so inefficient?

    BTW I am quite happy that #group_by is quasi a standard name for this.

    Cheers
    Robert
    --
    http://ruby-smalltalk.blogspot.com/

    ---
    Whereof one cannot speak, thereof one must be silent.
    Ludwig Wittgenstein
    Robert Dober, Apr 18, 2008
    #6
    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. =?Utf-8?B?bGFuZW0=?=

    ASP.NET 2.0 web page naming question

    =?Utf-8?B?bGFuZW0=?=, Oct 24, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    373
    =?Utf-8?B?bGFuZW0=?=
    Oct 24, 2005
  2. Jayaram
    Replies:
    1
    Views:
    680
    Sudsy
    Feb 28, 2004
  3. JamesY

    Question about Naming

    JamesY, Apr 27, 2005, in forum: Java
    Replies:
    0
    Views:
    306
    JamesY
    Apr 27, 2005
  4. Replies:
    0
    Views:
    391
  5. Harman
    Replies:
    1
    Views:
    2,492
    Moiristo
    Jul 28, 2006
Loading...

Share This Page