# Naming question

**URL:** <https://www.ruby-forum.com/t/naming-question/135131>\
**Category:** Ruby\
**Created:** [April 18, 2008, 9:38am UTC](https://www.ruby-forum.com/t/naming-question/135131 "2008-04-18T09:38:07Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![flatekmonkey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/e8c25b/32.png) [@flatekmonkey](https://www.ruby-forum.com/u/flatekmonkey)\
**Post date:** [April 18, 2008, 9:38am UTC](https://www.ruby-forum.com/t/naming-question/135131/1 "2008-04-18T09:38:07Z")

</div>

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/](http://ruby-smalltalk.blogspot.com/)

* * *

Whereof one cannot speak, thereof one must be silent.  
Ludwig Wittgenstein

---

<div class="post-metadata">

**Author:** ![flatekmonkey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/e8c25b/32.png) [@flatekmonkey](https://www.ruby-forum.com/u/flatekmonkey)\
**Post date:** [April 18, 2008, 9:45am UTC](https://www.ruby-forum.com/t/naming-question/135131/2 "2008-04-18T09:45:51Z")

</div>

On Fri, Apr 18, 2008 at 9:36 AM, Robert D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
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/](http://ruby-smalltalk.blogspot.com/)

* * *

Whereof one cannot speak, thereof one must be silent.  
Ludwig Wittgenstein

---

<div class="post-metadata">

**Author:** ![flatekmonkey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/e8c25b/32.png) [@flatekmonkey](https://www.ruby-forum.com/u/flatekmonkey)\
**Post date:** [April 18, 2008, 10:03am UTC](https://www.ruby-forum.com/t/naming-question/135131/3 "2008-04-18T10:03:33Z")

</div>

From: Robert D. [[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)]

# 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

i think this is #group\_by in 1.8.7 and 1.9 ?

kind regards -botp

---

<div class="post-metadata">

**Author:** ![flatekmonkey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/e8c25b/32.png) [@flatekmonkey](https://www.ruby-forum.com/u/flatekmonkey)\
**Post date:** [April 18, 2008, 1:11pm UTC](https://www.ruby-forum.com/t/naming-question/135131/4 "2008-04-18T13:11:29Z")

</div>

On Fri, Apr 18, 2008 at 4:02 AM, Peña, Botp [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> # 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.

–  
Rick DeNatale

My blog on Ruby  
[http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)

---

<div class="post-metadata">

**Author:** ![flatekmonkey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/e8c25b/32.png) [@flatekmonkey](https://www.ruby-forum.com/u/flatekmonkey)\
**Post date:** [April 18, 2008, 6:05pm UTC](https://www.ruby-forum.com/t/naming-question/135131/5 "2008-04-18T18:05:27Z")

</div>

On Fri, Apr 18, 2008 at 12:10 PM, Rick DeNatale  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Which, I believe, first showed up in Rails sometime in 2006.
> 
> –  
> Rick DeNatale

Florian G. published a version in Jan 2005:  
[http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-core/4311](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

---

<div class="post-metadata">

**Author:** ![flatekmonkey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/e8c25b/32.png) [@flatekmonkey](https://www.ruby-forum.com/u/flatekmonkey)\
**Post date:** [April 18, 2008, 6:34pm UTC](https://www.ruby-forum.com/t/naming-question/135131/6 "2008-04-18T18:34:59Z")

</div>

On Fri, Apr 18, 2008 at 6:04 PM, Sean O’Halpin [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> On Fri, Apr 18, 2008 at 12:10 PM, Rick DeNatale [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > Which, I believe, first showed up in Rails sometime in 2006.
> > 
> > –  
> > Rick DeNatale
> 
> Florian G. published a version in Jan 2005:  
> [http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-core/4311](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/](http://ruby-smalltalk.blogspot.com/)

* * *

Whereof one cannot speak, thereof one must be silent.  
Ludwig Wittgenstein
