ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
* [ruby-core:68932] [Ruby trunk - Feature #11076] [Open] Enumerable method count_by
       [not found] <redmine.issue-11076.20150419203854@ruby-lang.org>
@ 2015-04-19 20:38 ` bottiger
  2015-04-20  1:32 ` [ruby-core:68933] [Ruby trunk - Feature #11076] " shevegen
                   ` (27 subsequent siblings)
  28 siblings, 0 replies; 29+ messages in thread
From: bottiger @ 2015-04-19 20:38 UTC (permalink / raw)
  To: ruby-core

Issue #11076 has been reported by Harald Böttiger.

----------------------------------------
Feature #11076: Enumerable method count_by
https://bugs.ruby-lang.org/issues/11076

* Author: Harald Böttiger
* Status: Open
* Priority: Low
* Assignee: 
----------------------------------------
I very often use `Hash[array.group_by{|x|x}.map{|x,y|[x,y.size]}]`.

Would be nice with to have a method called `count_by`:

    array = ['aa', 'aA', 'bb', 'cc']
    p array.count_by(&:downcase) #=> {'aa'=>2,'bb'=>1,'cc'=>1}



-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 29+ messages in thread

* [ruby-core:68933] [Ruby trunk - Feature #11076] Enumerable method count_by
       [not found] <redmine.issue-11076.20150419203854@ruby-lang.org>
  2015-04-19 20:38 ` [ruby-core:68932] [Ruby trunk - Feature #11076] [Open] Enumerable method count_by bottiger
@ 2015-04-20  1:32 ` shevegen
  2015-04-20  6:50 ` [ruby-core:68934] " nobu
                   ` (26 subsequent siblings)
  28 siblings, 0 replies; 29+ messages in thread
From: shevegen @ 2015-04-20  1:32 UTC (permalink / raw)
  To: ruby-core

Issue #11076 has been updated by Robert A. Heiler.


Can you also add a sentence or two for documentation? :-)

It may lower the entry barrier for adding a method such as the above (I assume it must be documented by someone before it could be added).

----------------------------------------
Feature #11076: Enumerable method count_by
https://bugs.ruby-lang.org/issues/11076#change-52197

* Author: Harald Böttiger
* Status: Open
* Priority: Low
* Assignee: 
----------------------------------------
I very often use `Hash[array.group_by{|x|x}.map{|x,y|[x,y.size]}]`.

Would be nice with to have a method called `count_by`:

    array = ['aa', 'aA', 'bb', 'cc']
    p array.count_by(&:downcase) #=> {'aa'=>2,'bb'=>1,'cc'=>1}



-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 29+ messages in thread

* [ruby-core:68934] [Ruby trunk - Feature #11076] Enumerable method count_by
       [not found] <redmine.issue-11076.20150419203854@ruby-lang.org>
  2015-04-19 20:38 ` [ruby-core:68932] [Ruby trunk - Feature #11076] [Open] Enumerable method count_by bottiger
  2015-04-20  1:32 ` [ruby-core:68933] [Ruby trunk - Feature #11076] " shevegen
@ 2015-04-20  6:50 ` nobu
  2015-04-20  8:14 ` [ruby-core:68935] " duerst
                   ` (25 subsequent siblings)
  28 siblings, 0 replies; 29+ messages in thread
From: nobu @ 2015-04-20  6:50 UTC (permalink / raw)
  To: ruby-core

Issue #11076 has been updated by Nobuyoshi Nakada.

Description updated

https://github.com/ruby/ruby/compare/trunk...nobu:feature/11076-Enumerable%23count_by

----------------------------------------
Feature #11076: Enumerable method count_by
https://bugs.ruby-lang.org/issues/11076#change-52199

* Author: Harald Böttiger
* Status: Open
* Priority: Low
* Assignee: 
----------------------------------------
I very often use `Hash[array.group_by{|x|x}.map{|x,y|[x,y.size]}]`.

Would be nice with to have a method called `count_by`:

~~~ruby
array = ['aa', 'aA', 'bb', 'cc']
p array.count_by(&:downcase) #=> {'aa'=>2,'bb'=>1,'cc'=>1}
~~~



-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 29+ messages in thread

* [ruby-core:68935] [Ruby trunk - Feature #11076] Enumerable method count_by
       [not found] <redmine.issue-11076.20150419203854@ruby-lang.org>
                   ` (2 preceding siblings ...)
  2015-04-20  6:50 ` [ruby-core:68934] " nobu
@ 2015-04-20  8:14 ` duerst
  2015-04-20 14:08 ` [ruby-core:68938] " ko1
                   ` (24 subsequent siblings)
  28 siblings, 0 replies; 29+ messages in thread
From: duerst @ 2015-04-20  8:14 UTC (permalink / raw)
  To: ruby-core

Issue #11076 has been updated by Martin Dürst.


Having this would definitely be very useful. I remember having searched for a 'count_by' method more than once in the past.

----------------------------------------
Feature #11076: Enumerable method count_by
https://bugs.ruby-lang.org/issues/11076#change-52200

* Author: Harald Böttiger
* Status: Open
* Priority: Low
* Assignee: 
----------------------------------------
I very often use `Hash[array.group_by{|x|x}.map{|x,y|[x,y.size]}]`.

Would be nice with to have a method called `count_by`:

~~~ruby
array = ['aa', 'aA', 'bb', 'cc']
p array.count_by(&:downcase) #=> {'aa'=>2,'bb'=>1,'cc'=>1}
~~~



-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 29+ messages in thread

* [ruby-core:68938] [Ruby trunk - Feature #11076] Enumerable method count_by
       [not found] <redmine.issue-11076.20150419203854@ruby-lang.org>
                   ` (3 preceding siblings ...)
  2015-04-20  8:14 ` [ruby-core:68935] " duerst
@ 2015-04-20 14:08 ` ko1
  2015-04-20 15:35 ` [ruby-core:68941] " bottiger
                   ` (23 subsequent siblings)
  28 siblings, 0 replies; 29+ messages in thread
From: ko1 @ 2015-04-20 14:08 UTC (permalink / raw)
  To: ruby-core

Issue #11076 has been updated by Koichi Sasada.


+1

----------------------------------------
Feature #11076: Enumerable method count_by
https://bugs.ruby-lang.org/issues/11076#change-52202

* Author: Harald Böttiger
* Status: Open
* Priority: Low
* Assignee: 
----------------------------------------
I very often use `Hash[array.group_by{|x|x}.map{|x,y|[x,y.size]}]`.

Would be nice with to have a method called `count_by`:

~~~ruby
array = ['aa', 'aA', 'bb', 'cc']
p array.count_by(&:downcase) #=> {'aa'=>2,'bb'=>1,'cc'=>1}
~~~



-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 29+ messages in thread

* [ruby-core:68941] [Ruby trunk - Feature #11076] Enumerable method count_by
       [not found] <redmine.issue-11076.20150419203854@ruby-lang.org>
                   ` (4 preceding siblings ...)
  2015-04-20 14:08 ` [ruby-core:68938] " ko1
@ 2015-04-20 15:35 ` bottiger
  2018-06-29  1:01 ` [ruby-core:87673] [Ruby trunk Feature#11076] " keystonelemur
                   ` (22 subsequent siblings)
  28 siblings, 0 replies; 29+ messages in thread
From: bottiger @ 2015-04-20 15:35 UTC (permalink / raw)
  To: ruby-core

Issue #11076 has been updated by Harald Böttiger.


I am sorry but I am not sure to properly format this, but the documentation would be like:

    Syntax:
      group_by { |obj| block } → a_hash
      group_by → an_enumerator
    
    Description:
      Groups the collection by result of the block. Returns a hash where the keys are the evaluated result from the block and the values are the number of the elements in the collection that correspond to the key.
    
      If no block is given an enumerator is returned.
    
    Examples:
      ['a','a','a','b','c'].group_by { |x| x } #=> {'a'=>3, 'b'=>1, 'c'=>1}
      (1..7).group_by { |i| i%3 }   #=> {0=>2, 1=>3, 2=>2}


----------------------------------------
Feature #11076: Enumerable method count_by
https://bugs.ruby-lang.org/issues/11076#change-52204

* Author: Harald Böttiger
* Status: Open
* Priority: Low
* Assignee: 
----------------------------------------
I very often use `Hash[array.group_by{|x|x}.map{|x,y|[x,y.size]}]`.

Would be nice with to have a method called `count_by`:

~~~ruby
array = ['aa', 'aA', 'bb', 'cc']
p array.count_by(&:downcase) #=> {'aa'=>2,'bb'=>1,'cc'=>1}
~~~



-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 29+ messages in thread

* [ruby-core:87673] [Ruby trunk Feature#11076] Enumerable method count_by
       [not found] <redmine.issue-11076.20150419203854@ruby-lang.org>
                   ` (5 preceding siblings ...)
  2015-04-20 15:35 ` [ruby-core:68941] " bottiger
@ 2018-06-29  1:01 ` keystonelemur
  2018-08-09  7:58 ` [ruby-core:88373] " knu
                   ` (21 subsequent siblings)
  28 siblings, 0 replies; 29+ messages in thread
From: keystonelemur @ 2018-06-29  1:01 UTC (permalink / raw)
  To: ruby-core

Issue #11076 has been updated by baweaver (Brandon Weaver).


Has there been any thought on this as a language feature?

There was an earlier conversation demonstrating a practical use for this feature, and I had mentioned a few of the core maintainers to bring the subject back into consideration:

https://twitter.com/keystonelemur/status/1012434696909852672

nobu had recently updated his patch here:

https://github.com/ruby/ruby/compare/trunk...nobu:feature/11076-Enumerable%23count_by

I still believe this would be an incredibly useful feature to have in the core of the language, as a very common pattern to work around it is unintuitive for newer programmers:

```
# Most common
array
  .group_by { |v| v }
  .map { |k, v| [k, v.size] }
  .to_h

# In older versions:
Hash[array.group_by { |v| v }.map { |k, v| [k, v.size] }]

# or in more recent versions:
array
  .group_by { |v| v }
  .transform_values(&:size)

# or using reduce / ewo:
array.each_with_object(Hash.new(0)) { |v, h| h[v] += 1 }
```

By giving a name to this concept, we've made it more accessible as well. Given the current trend of 2.6, I believe this would be a welcome addition.

----------------------------------------
Feature #11076: Enumerable method count_by
https://bugs.ruby-lang.org/issues/11076#change-72696

* Author: haraldb (Harald Böttiger)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
----------------------------------------
I very often use `Hash[array.group_by{|x|x}.map{|x,y|[x,y.size]}]`.

Would be nice with to have a method called `count_by`:

~~~ruby
array = ['aa', 'aA', 'bb', 'cc']
p array.count_by(&:downcase) #=> {'aa'=>2,'bb'=>1,'cc'=>1}
~~~



-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 29+ messages in thread

* [ruby-core:88373] [Ruby trunk Feature#11076] Enumerable method count_by
       [not found] <redmine.issue-11076.20150419203854@ruby-lang.org>
                   ` (6 preceding siblings ...)
  2018-06-29  1:01 ` [ruby-core:87673] [Ruby trunk Feature#11076] " keystonelemur
@ 2018-08-09  7:58 ` knu
  2018-08-09 18:15 ` [ruby-core:88403] " keystonelemur
                   ` (20 subsequent siblings)
  28 siblings, 0 replies; 29+ messages in thread
From: knu @ 2018-08-09  7:58 UTC (permalink / raw)
  To: ruby-core

Issue #11076 has been updated by knu (Akinori MUSHA).


In today's developer meeting, Matz understood the need for the feature but didn't like the name.  One point he made was that existing pairs like sort/sort_by and max/max_by share their features, count_by() might not go well with count().

----------------------------------------
Feature #11076: Enumerable method count_by
https://bugs.ruby-lang.org/issues/11076#change-73413

* Author: haraldb (Harald Böttiger)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
----------------------------------------
I very often use `Hash[array.group_by{|x|x}.map{|x,y|[x,y.size]}]`.

Would be nice with to have a method called `count_by`:

~~~ruby
array = ['aa', 'aA', 'bb', 'cc']
p array.count_by(&:downcase) #=> {'aa'=>2,'bb'=>1,'cc'=>1}
~~~



-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 29+ messages in thread

* [ruby-core:88403] [Ruby trunk Feature#11076] Enumerable method count_by
       [not found] <redmine.issue-11076.20150419203854@ruby-lang.org>
                   ` (7 preceding siblings ...)
  2018-08-09  7:58 ` [ruby-core:88373] " knu
@ 2018-08-09 18:15 ` keystonelemur
  2018-08-10 11:37 ` [ruby-core:88436] " janfri26
                   ` (19 subsequent siblings)
  28 siblings, 0 replies; 29+ messages in thread
From: keystonelemur @ 2018-08-09 18:15 UTC (permalink / raw)
  To: ruby-core

Issue #11076 has been updated by baweaver (Brandon Weaver).


`group_count`? It's half-way between `group_by` and `count`

----------------------------------------
Feature #11076: Enumerable method count_by
https://bugs.ruby-lang.org/issues/11076#change-73455

* Author: haraldb (Harald Böttiger)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
----------------------------------------
I very often use `Hash[array.group_by{|x|x}.map{|x,y|[x,y.size]}]`.

Would be nice with to have a method called `count_by`:

~~~ruby
array = ['aa', 'aA', 'bb', 'cc']
p array.count_by(&:downcase) #=> {'aa'=>2,'bb'=>1,'cc'=>1}
~~~



-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 29+ messages in thread

* [ruby-core:88436] [Ruby trunk Feature#11076] Enumerable method count_by
       [not found] <redmine.issue-11076.20150419203854@ruby-lang.org>
                   ` (8 preceding siblings ...)
  2018-08-09 18:15 ` [ruby-core:88403] " keystonelemur
@ 2018-08-10 11:37 ` janfri26
  2018-08-21 17:55 ` [ruby-core:88598] " dgjones
                   ` (18 subsequent siblings)
  28 siblings, 0 replies; 29+ messages in thread
From: janfri26 @ 2018-08-10 11:37 UTC (permalink / raw)
  To: ruby-core

Issue #11076 has been updated by janfri (Jan Friedrich).


As Naruse in DevelopersMeeting20180809 mentioned: It is similar to a histogram function.
How about ```histogram_by``` (and for the block-less counterpart ```histogram```)?

----------------------------------------
Feature #11076: Enumerable method count_by
https://bugs.ruby-lang.org/issues/11076#change-73491

* Author: haraldb (Harald Böttiger)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
----------------------------------------
I very often use `Hash[array.group_by{|x|x}.map{|x,y|[x,y.size]}]`.

Would be nice with to have a method called `count_by`:

~~~ruby
array = ['aa', 'aA', 'bb', 'cc']
p array.count_by(&:downcase) #=> {'aa'=>2,'bb'=>1,'cc'=>1}
~~~



-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 29+ messages in thread

* [ruby-core:88598] [Ruby trunk Feature#11076] Enumerable method count_by
       [not found] <redmine.issue-11076.20150419203854@ruby-lang.org>
                   ` (9 preceding siblings ...)
  2018-08-10 11:37 ` [ruby-core:88436] " janfri26
@ 2018-08-21 17:55 ` dgjones
  2018-12-13  0:23 ` [ruby-core:90462] " keystonelemur
                   ` (17 subsequent siblings)
  28 siblings, 0 replies; 29+ messages in thread
From: dgjones @ 2018-08-21 17:55 UTC (permalink / raw)
  To: ruby-core

Issue #11076 has been updated by djones (David Jones).


How about `tally`?

```ruby
array = ['aa', 'aA', 'bb', 'cc']
p array.tally(&:downcase) #=> {'aa'=>2,'bb'=>1,'cc'=>1}
```

`tally` describes quite well to me what this method does and avoids clashing with `group` or `count`.
`tally_by` might be worthy of consideration too.

## Definition of "Tally"

Current score or amount: _that takes his tally to 10 goals in 10 games._

  1. a record of a score or amount: _I kept a running tally of David's debt on a note above my desk._
  2. **a particular number taken as a group or unit to facilitate counting.**
  3. a mark registering a number or amount.
  4. an account kept by means of a tally.

----------------------------------------
Feature #11076: Enumerable method count_by
https://bugs.ruby-lang.org/issues/11076#change-73654

* Author: haraldb (Harald Böttiger)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
----------------------------------------
I very often use `Hash[array.group_by{|x|x}.map{|x,y|[x,y.size]}]`.

Would be nice with to have a method called `count_by`:

~~~ruby
array = ['aa', 'aA', 'bb', 'cc']
p array.count_by(&:downcase) #=> {'aa'=>2,'bb'=>1,'cc'=>1}
~~~



-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 29+ messages in thread

* [ruby-core:90462] [Ruby trunk Feature#11076] Enumerable method count_by
       [not found] <redmine.issue-11076.20150419203854@ruby-lang.org>
                   ` (10 preceding siblings ...)
  2018-08-21 17:55 ` [ruby-core:88598] " dgjones
@ 2018-12-13  0:23 ` keystonelemur
  2018-12-13 10:32 ` [ruby-core:90502] " janfri26
                   ` (16 subsequent siblings)
  28 siblings, 0 replies; 29+ messages in thread
From: keystonelemur @ 2018-12-13  0:23 UTC (permalink / raw)
  To: ruby-core

Issue #11076 has been updated by baweaver (Brandon Weaver).


@matz / @ko1: Any chance of this making it into 2.6? The code is already done (thanks @nobu) and the only consideration left is the name. Would `tally_by` be an acceptable compromise?

----------------------------------------
Feature #11076: Enumerable method count_by
https://bugs.ruby-lang.org/issues/11076#change-75622

* Author: haraldb (Harald Böttiger)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
----------------------------------------
I very often use `Hash[array.group_by{|x|x}.map{|x,y|[x,y.size]}]`.

Would be nice with to have a method called `count_by`:

~~~ruby
array = ['aa', 'aA', 'bb', 'cc']
p array.count_by(&:downcase) #=> {'aa'=>2,'bb'=>1,'cc'=>1}
~~~



-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 29+ messages in thread

* [ruby-core:90502] [Ruby trunk Feature#11076] Enumerable method count_by
       [not found] <redmine.issue-11076.20150419203854@ruby-lang.org>
                   ` (11 preceding siblings ...)
  2018-12-13  0:23 ` [ruby-core:90462] " keystonelemur
@ 2018-12-13 10:32 ` janfri26
  2018-12-14 17:35 ` [ruby-core:90529] " oliverp
                   ` (15 subsequent siblings)
  28 siblings, 0 replies; 29+ messages in thread
From: janfri26 @ 2018-12-13 10:32 UTC (permalink / raw)
  To: ruby-core

Issue #11076 has been updated by janfri (Jan Friedrich).


Just my 2 cents: I'm not a native English speaker. Never heard the word "tally" before. So I would never remember it and has always to look at the api docs.

----------------------------------------
Feature #11076: Enumerable method count_by
https://bugs.ruby-lang.org/issues/11076#change-75660

* Author: haraldb (Harald Böttiger)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
----------------------------------------
I very often use `Hash[array.group_by{|x|x}.map{|x,y|[x,y.size]}]`.

Would be nice with to have a method called `count_by`:

~~~ruby
array = ['aa', 'aA', 'bb', 'cc']
p array.count_by(&:downcase) #=> {'aa'=>2,'bb'=>1,'cc'=>1}
~~~



-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 29+ messages in thread

* [ruby-core:90529] [Ruby trunk Feature#11076] Enumerable method count_by
       [not found] <redmine.issue-11076.20150419203854@ruby-lang.org>
                   ` (12 preceding siblings ...)
  2018-12-13 10:32 ` [ruby-core:90502] " janfri26
@ 2018-12-14 17:35 ` oliverp
  2019-01-24  4:22 ` [ruby-core:91244] " joshua.goodall
                   ` (14 subsequent siblings)
  28 siblings, 0 replies; 29+ messages in thread
From: oliverp @ 2018-12-14 17:35 UTC (permalink / raw)
  To: ruby-core

Issue #11076 has been updated by odlp (Oliver Peate).


For me the definition of tally does seem to fit the use case, so +1 to `tally(_by)`.

One other suggestion - how about [census](https://www.merriam-webster.com/dictionary/census) (as in `census_by(&:downcase)`)? It's more widely used than tally (although I think tally is the better choice): 

[https://books.google.com/ngrams/graph?content=tally%2Ccensus%2Ccount&case_insensitive=on&year_start=1900&year_end=2008](https://books.google.com/ngrams/graph?content=tally%2Ccensus%2Ccount&case_insensitive=on&year_start=1900&year_end=2008)

----------------------------------------
Feature #11076: Enumerable method count_by
https://bugs.ruby-lang.org/issues/11076#change-75683

* Author: haraldb (Harald Böttiger)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
----------------------------------------
I very often use `Hash[array.group_by{|x|x}.map{|x,y|[x,y.size]}]`.

Would be nice with to have a method called `count_by`:

~~~ruby
array = ['aa', 'aA', 'bb', 'cc']
p array.count_by(&:downcase) #=> {'aa'=>2,'bb'=>1,'cc'=>1}
~~~



-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 29+ messages in thread

* [ruby-core:91244] [Ruby trunk Feature#11076] Enumerable method count_by
       [not found] <redmine.issue-11076.20150419203854@ruby-lang.org>
                   ` (13 preceding siblings ...)
  2018-12-14 17:35 ` [ruby-core:90529] " oliverp
@ 2019-01-24  4:22 ` joshua.goodall
  2019-01-24 14:21 ` [ruby-core:91252] " mame
                   ` (13 subsequent siblings)
  28 siblings, 0 replies; 29+ messages in thread
From: joshua.goodall @ 2019-01-24  4:22 UTC (permalink / raw)
  To: ruby-core

Issue #11076 has been updated by inopinatus (Joshua GOODALL).


A histogram refers to counts of items in ranges of otherwise continuous data. But this function is more general than that, so I think `histogram` is too specific a term.

For this native English speaker, `tally` is the most precisely fitted method name.


----------------------------------------
Feature #11076: Enumerable method count_by
https://bugs.ruby-lang.org/issues/11076#change-76489

* Author: haraldb (Harald Böttiger)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
----------------------------------------
I very often use `Hash[array.group_by{|x|x}.map{|x,y|[x,y.size]}]`.

Would be nice with to have a method called `count_by`:

~~~ruby
array = ['aa', 'aA', 'bb', 'cc']
p array.count_by(&:downcase) #=> {'aa'=>2,'bb'=>1,'cc'=>1}
~~~



-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 29+ messages in thread

* [ruby-core:91252] [Ruby trunk Feature#11076] Enumerable method count_by
       [not found] <redmine.issue-11076.20150419203854@ruby-lang.org>
                   ` (14 preceding siblings ...)
  2019-01-24  4:22 ` [ruby-core:91244] " joshua.goodall
@ 2019-01-24 14:21 ` mame
  2019-01-25  0:11 ` [ruby-core:91254] " muraken
                   ` (12 subsequent siblings)
  28 siblings, 0 replies; 29+ messages in thread
From: mame @ 2019-01-24 14:21 UTC (permalink / raw)
  To: ruby-core

Issue #11076 has been updated by mame (Yusuke Endoh).


I have learnt the word "tally" in this thread.  Thank you.  It looks good to me, a non-native speaker. I have put this on the agenda of the next developers' meeting.

By the way, what is the precise semantics of the method?

Question 1. What identity is the object in the keys?

```
str1 = "a"
str2 = "a"
t = [str1, str2].tally

p t  #=> { "a" => 2 }

p t.keys.first.object_id #=> str1.object_id or str2.object_id ?
```

IMO: I think it should prefer the first element, so it should be equal to `str1.object_id`.

Question 2. What is the key of `tally_by`?

```
str1 = "a"
str2 = "A"
t = [str1, str2].tally_by(&:upcase)

p t  #=> { "a" => 2 } or { "A" => 2 } ?

p t.keys.first.object_id #=> str1.object_id, str2.object_id, or otherwise?
```

IMO: The return values of `sort_by` and `max_by` contains the original elements, not the return value of the block.  According to the analogy to them, I think that `t` should be `{ "a" => 2 }` and its key be `str1.object_id`.

----------------------------------------
Feature #11076: Enumerable method count_by
https://bugs.ruby-lang.org/issues/11076#change-76498

* Author: haraldb (Harald Böttiger)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
----------------------------------------
I very often use `Hash[array.group_by{|x|x}.map{|x,y|[x,y.size]}]`.

Would be nice with to have a method called `count_by`:

~~~ruby
array = ['aa', 'aA', 'bb', 'cc']
p array.count_by(&:downcase) #=> {'aa'=>2,'bb'=>1,'cc'=>1}
~~~



-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 29+ messages in thread

* [ruby-core:91254] [Ruby trunk Feature#11076] Enumerable method count_by
       [not found] <redmine.issue-11076.20150419203854@ruby-lang.org>
                   ` (15 preceding siblings ...)
  2019-01-24 14:21 ` [ruby-core:91252] " mame
@ 2019-01-25  0:11 ` muraken
  2019-01-29  1:05 ` [ruby-core:91312] " keystonelemur
                   ` (11 subsequent siblings)
  28 siblings, 0 replies; 29+ messages in thread
From: muraken @ 2019-01-25  0:11 UTC (permalink / raw)
  To: ruby-core

Issue #11076 has been updated by mrkn (Kenta Murata).


enumerable-statistics provides `value_counts` method.
https://github.com/mrkn/enumerable-statistics/blob/master/ext/enumerable/statistics/extension/statistics.c#L1651-L1668
 It is designed to follow pandas’s `Series.value_counts`.

----------------------------------------
Feature #11076: Enumerable method count_by
https://bugs.ruby-lang.org/issues/11076#change-76501

* Author: haraldb (Harald Böttiger)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
----------------------------------------
I very often use `Hash[array.group_by{|x|x}.map{|x,y|[x,y.size]}]`.

Would be nice with to have a method called `count_by`:

~~~ruby
array = ['aa', 'aA', 'bb', 'cc']
p array.count_by(&:downcase) #=> {'aa'=>2,'bb'=>1,'cc'=>1}
~~~



-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 29+ messages in thread

* [ruby-core:91312] [Ruby trunk Feature#11076] Enumerable method count_by
       [not found] <redmine.issue-11076.20150419203854@ruby-lang.org>
                   ` (16 preceding siblings ...)
  2019-01-25  0:11 ` [ruby-core:91254] " muraken
@ 2019-01-29  1:05 ` keystonelemur
  2019-01-29  2:52 ` [ruby-core:91314] " nobu
                   ` (10 subsequent siblings)
  28 siblings, 0 replies; 29+ messages in thread
From: keystonelemur @ 2019-01-29  1:05 UTC (permalink / raw)
  To: ruby-core

Issue #11076 has been updated by baweaver (Brandon Weaver).


mame (Yusuke Endoh) wrote:
> I have learnt the word "tally" in this thread.  Thank you.  It looks good to me, a non-native speaker. I have put this on the agenda of the next developers' meeting.
> 
> By the way, what is the precise semantics of the method?
> 
> Question 1. What identity is the object in the keys?
> 
> ```
> str1 = "a"
> str2 = "a"
> t = [str1, str2].tally
> 
> p t  #=> { "a" => 2 }
> 
> p t.keys.first.object_id #=> str1.object_id or str2.object_id ?
> ```
> 
> IMO: I think it should prefer the first element, so it should be equal to `str1.object_id`.
> 
> Question 2. What is the key of `tally_by`?
> 
> ```
> str1 = "a"
> str2 = "A"
> t = [str1, str2].tally_by(&:upcase)
> 
> p t  #=> { "a" => 2 } or { "A" => 2 } ?
> 
> p t.keys.first.object_id #=> str1.object_id, str2.object_id, or otherwise?
> ```
> 
> IMO: The return values of `sort_by` and `max_by` contains the original elements, not the return value of the block.  According to the analogy to them, I think that `t` should be `{ "a" => 2 }` and its key be `str1.object_id`.

Answer 1: I would say the first, but `tally` could also be effectively represented by `tally_by(&:itself)` as shown in an implementation below:

Answer 2: The transformed value, like `group_by`:

```
[1, 2, 3].group_by(&:even?)
=> {false=>[1, 3], true=>[2]}

[1, 2, 3].tally_by(&:even?)
=> {false => 2, true => 1}
```

The implementation is similar to this:

```
module Enumerable
  # Implementing via group_by
  def tally_by(&fn)
    group_by(&fn).to_h { |k, vs| [k, vs.size] }
  end

  # Implementing via reduction
  def tally_by2(&fn)
    each_with_object(Hash.new(0)) { |v, a| a[fn[v]] += 1 }
  end
end
```

...which would result in the first `object_id` I believe.

----------------------------------------
Feature #11076: Enumerable method count_by
https://bugs.ruby-lang.org/issues/11076#change-76559

* Author: haraldb (Harald Böttiger)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
----------------------------------------
I very often use `Hash[array.group_by{|x|x}.map{|x,y|[x,y.size]}]`.

Would be nice with to have a method called `count_by`:

~~~ruby
array = ['aa', 'aA', 'bb', 'cc']
p array.count_by(&:downcase) #=> {'aa'=>2,'bb'=>1,'cc'=>1}
~~~



-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 29+ messages in thread

* [ruby-core:91314] [Ruby trunk Feature#11076] Enumerable method count_by
       [not found] <redmine.issue-11076.20150419203854@ruby-lang.org>
                   ` (17 preceding siblings ...)
  2019-01-29  1:05 ` [ruby-core:91312] " keystonelemur
@ 2019-01-29  2:52 ` nobu
  2019-01-29 10:42 ` [ruby-core:91317] " eregontp
                   ` (9 subsequent siblings)
  28 siblings, 0 replies; 29+ messages in thread
From: nobu @ 2019-01-29  2:52 UTC (permalink / raw)
  To: ruby-core

Issue #11076 has been updated by nobu (Nobuyoshi Nakada).


https://github.com/nobu/ruby/pull/new/feature/11076-Enumerable%23tally

As `Hash#[]=` copies string keys, the `object_id` will be unique unless the item is frozen.

----------------------------------------
Feature #11076: Enumerable method count_by
https://bugs.ruby-lang.org/issues/11076#change-76561

* Author: haraldb (Harald Böttiger)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
----------------------------------------
I very often use `Hash[array.group_by{|x|x}.map{|x,y|[x,y.size]}]`.

Would be nice with to have a method called `count_by`:

~~~ruby
array = ['aa', 'aA', 'bb', 'cc']
p array.count_by(&:downcase) #=> {'aa'=>2,'bb'=>1,'cc'=>1}
~~~



-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 29+ messages in thread

* [ruby-core:91317] [Ruby trunk Feature#11076] Enumerable method count_by
       [not found] <redmine.issue-11076.20150419203854@ruby-lang.org>
                   ` (18 preceding siblings ...)
  2019-01-29  2:52 ` [ruby-core:91314] " nobu
@ 2019-01-29 10:42 ` eregontp
  2019-02-02  3:01 ` [ruby-core:91380] " sawadatsuyoshi
                   ` (8 subsequent siblings)
  28 siblings, 0 replies; 29+ messages in thread
From: eregontp @ 2019-01-29 10:42 UTC (permalink / raw)
  To: ruby-core

Issue #11076 has been updated by Eregon (Benoit Daloze).


For this kind of method, I wish we would implement it in Ruby even in MRI: it's much simpler, more readable, and every Ruby implementation could use it.

----------------------------------------
Feature #11076: Enumerable method count_by
https://bugs.ruby-lang.org/issues/11076#change-76567

* Author: haraldb (Harald Böttiger)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
----------------------------------------
I very often use `Hash[array.group_by{|x|x}.map{|x,y|[x,y.size]}]`.

Would be nice with to have a method called `count_by`:

~~~ruby
array = ['aa', 'aA', 'bb', 'cc']
p array.count_by(&:downcase) #=> {'aa'=>2,'bb'=>1,'cc'=>1}
~~~



-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 29+ messages in thread

* [ruby-core:91380] [Ruby trunk Feature#11076] Enumerable method count_by
       [not found] <redmine.issue-11076.20150419203854@ruby-lang.org>
                   ` (19 preceding siblings ...)
  2019-01-29 10:42 ` [ruby-core:91317] " eregontp
@ 2019-02-02  3:01 ` sawadatsuyoshi
  2019-02-02  6:21 ` [ruby-core:91381] " duerst
                   ` (7 subsequent siblings)
  28 siblings, 0 replies; 29+ messages in thread
From: sawadatsuyoshi @ 2019-02-02  3:01 UTC (permalink / raw)
  To: ruby-core

Issue #11076 has been updated by sawa (Tsuyoshi Sawada).


knu (Akinori MUSHA) wrote:
> In today's developer meeting, Matz understood the need for the feature but didn't like the name.  One point he made was that existing pairs like sort/sort_by and max/max_by share their features, so count_by() might not go well with count().

Since this feature is an inferior variant of `group_by` in the sense that it reduces the value arrays into their lengths, what about naming the method `group`?

----------------------------------------
Feature #11076: Enumerable method count_by
https://bugs.ruby-lang.org/issues/11076#change-76635

* Author: haraldb (Harald Böttiger)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
----------------------------------------
I very often use `Hash[array.group_by{|x|x}.map{|x,y|[x,y.size]}]`.

Would be nice with to have a method called `count_by`:

~~~ruby
array = ['aa', 'aA', 'bb', 'cc']
p array.count_by(&:downcase) #=> {'aa'=>2,'bb'=>1,'cc'=>1}
~~~



-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 29+ messages in thread

* [ruby-core:91381] [Ruby trunk Feature#11076] Enumerable method count_by
       [not found] <redmine.issue-11076.20150419203854@ruby-lang.org>
                   ` (20 preceding siblings ...)
  2019-02-02  3:01 ` [ruby-core:91380] " sawadatsuyoshi
@ 2019-02-02  6:21 ` duerst
  2019-02-06 13:04 ` [ruby-core:91429] " mame
                   ` (6 subsequent siblings)
  28 siblings, 0 replies; 29+ messages in thread
From: duerst @ 2019-02-02  6:21 UTC (permalink / raw)
  To: ruby-core

Issue #11076 has been updated by duerst (Martin Dürst).


sawa (Tsuyoshi Sawada) wrote:

> Since this feature is an inferior variant of `group_by` in the sense that it reduces the value arrays into their lengths, what about naming the method `group`?

Please not. The `_by` indicates that there is some specific criterion for grouping. This is the same for this method, so removing the `_by` is very strange. Also, the fact that the result contains numbers, not the actual groups, is completely lost.

Compared with this, `count_by` is much better, and so is `tally`. Other possibilities might be `group_by_and_count` or `count_by_group` or something similar.

----------------------------------------
Feature #11076: Enumerable method count_by
https://bugs.ruby-lang.org/issues/11076#change-76636

* Author: haraldb (Harald Böttiger)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
----------------------------------------
I very often use `Hash[array.group_by{|x|x}.map{|x,y|[x,y.size]}]`.

Would be nice with to have a method called `count_by`:

~~~ruby
array = ['aa', 'aA', 'bb', 'cc']
p array.count_by(&:downcase) #=> {'aa'=>2,'bb'=>1,'cc'=>1}
~~~



-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 29+ messages in thread

* [ruby-core:91429] [Ruby trunk Feature#11076] Enumerable method count_by
       [not found] <redmine.issue-11076.20150419203854@ruby-lang.org>
                   ` (21 preceding siblings ...)
  2019-02-02  6:21 ` [ruby-core:91381] " duerst
@ 2019-02-06 13:04 ` mame
  2019-02-07  7:47 ` [ruby-core:91460] " matz
                   ` (5 subsequent siblings)
  28 siblings, 0 replies; 29+ messages in thread
From: mame @ 2019-02-06 13:04 UTC (permalink / raw)
  To: ruby-core

Issue #11076 has been updated by mame (Yusuke Endoh).


baweaver (Brandon Weaver) wrote:
> Answer 2: The transformed value, like `group_by`:
> 
> ```ruby
> [1, 2, 3].group_by(&:even?)
> => {false=>[1, 3], true=>[2]}
> 
> [1, 2, 3].tally_by(&:even?)
> => {false => 2, true => 1}
> ```

If we have `tally`, we can implement this behavior easily: `[1, 2, 3].map {|x| x.even? }.tally`.  Is a new method really needed just for a shorthand of this behavior?

----------------------------------------
Feature #11076: Enumerable method count_by
https://bugs.ruby-lang.org/issues/11076#change-76688

* Author: haraldb (Harald Böttiger)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
----------------------------------------
I very often use `Hash[array.group_by{|x|x}.map{|x,y|[x,y.size]}]`.

Would be nice with to have a method called `count_by`:

~~~ruby
array = ['aa', 'aA', 'bb', 'cc']
p array.count_by(&:downcase) #=> {'aa'=>2,'bb'=>1,'cc'=>1}
~~~



-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 29+ messages in thread

* [ruby-core:91460] [Ruby trunk Feature#11076] Enumerable method count_by
       [not found] <redmine.issue-11076.20150419203854@ruby-lang.org>
                   ` (22 preceding siblings ...)
  2019-02-06 13:04 ` [ruby-core:91429] " mame
@ 2019-02-07  7:47 ` matz
  2019-02-07  7:51 ` [ruby-core:91462] " mame
                   ` (4 subsequent siblings)
  28 siblings, 0 replies; 29+ messages in thread
From: matz @ 2019-02-07  7:47 UTC (permalink / raw)
  To: ruby-core

Issue #11076 has been updated by matz (Yukihiro Matsumoto).


OK, `tally` sounds reasonable.  Accepted.

Matz.


----------------------------------------
Feature #11076: Enumerable method count_by
https://bugs.ruby-lang.org/issues/11076#change-76721

* Author: haraldb (Harald Böttiger)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
----------------------------------------
I very often use `Hash[array.group_by{|x|x}.map{|x,y|[x,y.size]}]`.

Would be nice with to have a method called `count_by`:

~~~ruby
array = ['aa', 'aA', 'bb', 'cc']
p array.count_by(&:downcase) #=> {'aa'=>2,'bb'=>1,'cc'=>1}
~~~



-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 29+ messages in thread

* [ruby-core:91462] [Ruby trunk Feature#11076] Enumerable method count_by
       [not found] <redmine.issue-11076.20150419203854@ruby-lang.org>
                   ` (23 preceding siblings ...)
  2019-02-07  7:47 ` [ruby-core:91460] " matz
@ 2019-02-07  7:51 ` mame
  2019-02-07  8:10 ` [ruby-core:91465] " mame
                   ` (3 subsequent siblings)
  28 siblings, 0 replies; 29+ messages in thread
From: mame @ 2019-02-07  7:51 UTC (permalink / raw)
  To: ruby-core

Issue #11076 has been updated by mame (Yusuke Endoh).

Assignee set to mame (Yusuke Endoh)
Status changed from Open to Assigned

Thanks, I'll implement it.

Note that `tally_by` is not accepted yet.  We need to discuss the detail later (if needed).

----------------------------------------
Feature #11076: Enumerable method count_by
https://bugs.ruby-lang.org/issues/11076#change-76723

* Author: haraldb (Harald Böttiger)
* Status: Assigned
* Priority: Normal
* Assignee: mame (Yusuke Endoh)
* Target version: 
----------------------------------------
I very often use `Hash[array.group_by{|x|x}.map{|x,y|[x,y.size]}]`.

Would be nice with to have a method called `count_by`:

~~~ruby
array = ['aa', 'aA', 'bb', 'cc']
p array.count_by(&:downcase) #=> {'aa'=>2,'bb'=>1,'cc'=>1}
~~~



-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 29+ messages in thread

* [ruby-core:91465] [Ruby trunk Feature#11076] Enumerable method count_by
       [not found] <redmine.issue-11076.20150419203854@ruby-lang.org>
                   ` (24 preceding siblings ...)
  2019-02-07  7:51 ` [ruby-core:91462] " mame
@ 2019-02-07  8:10 ` mame
  2019-02-14 19:36 ` [ruby-core:91548] " keystonelemur
                   ` (2 subsequent siblings)
  28 siblings, 0 replies; 29+ messages in thread
From: mame @ 2019-02-07  8:10 UTC (permalink / raw)
  To: ruby-core

Issue #11076 has been updated by mame (Yusuke Endoh).

Assignee changed from mame (Yusuke Endoh) to nobu (Nobuyoshi Nakada)

Nobu has already started creating a patch.  Leave it to him.

----------------------------------------
Feature #11076: Enumerable method count_by
https://bugs.ruby-lang.org/issues/11076#change-76726

* Author: haraldb (Harald Böttiger)
* Status: Assigned
* Priority: Normal
* Assignee: nobu (Nobuyoshi Nakada)
* Target version: 
----------------------------------------
I very often use `Hash[array.group_by{|x|x}.map{|x,y|[x,y.size]}]`.

Would be nice with to have a method called `count_by`:

~~~ruby
array = ['aa', 'aA', 'bb', 'cc']
p array.count_by(&:downcase) #=> {'aa'=>2,'bb'=>1,'cc'=>1}
~~~



-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 29+ messages in thread

* [ruby-core:91548] [Ruby trunk Feature#11076] Enumerable method count_by
       [not found] <redmine.issue-11076.20150419203854@ruby-lang.org>
                   ` (25 preceding siblings ...)
  2019-02-07  8:10 ` [ruby-core:91465] " mame
@ 2019-02-14 19:36 ` keystonelemur
  2019-02-15  0:30 ` [ruby-core:91549] " mame
  2019-05-02 14:41 ` [ruby-core:92526] " jonathan
  28 siblings, 0 replies; 29+ messages in thread
From: keystonelemur @ 2019-02-14 19:36 UTC (permalink / raw)
  To: ruby-core

Issue #11076 has been updated by baweaver (Brandon Weaver).


mame (Yusuke Endoh) wrote:
> baweaver (Brandon Weaver) wrote:
> > Answer 2: The transformed value, like `group_by`:
> > 
> > ```ruby
> > [1, 2, 3].group_by(&:even?)
> > => {false=>[1, 3], true=>[2]}
> > 
> > [1, 2, 3].tally_by(&:even?)
> > => {false => 2, true => 1}
> > ```
> 
> If we have `tally`, we can implement this behavior easily: `[1, 2, 3].map {|x| x.even? }.tally`.  Is a new method really needed just for a shorthand of this behavior?

It's a common enough that the syntax may be justified. It could be argued that a lot of shorthand expressions aren't technically necessary, but I feel that this makes Ruby Ruby, the ability to say something common with less.

That, and there's established precedent of `count` / `count_by`, `max` / `max_by`, and others that would make this an easily adopted syntax. If it's not adopted I would not be surprised to see a follow-up request to add it.

I would see `tally_by` and other `*_by` methods as the base for their counterparts, such that:

```
[1,2,3].tally == [1,2,3].tally_by(&:itself)
```

Where the non-`*_by` method is effectively the `*_by` method implemented with the `itself` identity function.

----------------------------------------
Feature #11076: Enumerable method count_by
https://bugs.ruby-lang.org/issues/11076#change-76811

* Author: haraldb (Harald Böttiger)
* Status: Closed
* Priority: Normal
* Assignee: nobu (Nobuyoshi Nakada)
* Target version: 
----------------------------------------
I very often use `Hash[array.group_by{|x|x}.map{|x,y|[x,y.size]}]`.

Would be nice with to have a method called `count_by`:

~~~ruby
array = ['aa', 'aA', 'bb', 'cc']
p array.count_by(&:downcase) #=> {'aa'=>2,'bb'=>1,'cc'=>1}
~~~



-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 29+ messages in thread

* [ruby-core:91549] [Ruby trunk Feature#11076] Enumerable method count_by
       [not found] <redmine.issue-11076.20150419203854@ruby-lang.org>
                   ` (26 preceding siblings ...)
  2019-02-14 19:36 ` [ruby-core:91548] " keystonelemur
@ 2019-02-15  0:30 ` mame
  2019-05-02 14:41 ` [ruby-core:92526] " jonathan
  28 siblings, 0 replies; 29+ messages in thread
From: mame @ 2019-02-15  0:30 UTC (permalink / raw)
  To: ruby-core

Issue #11076 has been updated by mame (Yusuke Endoh).


baweaver (Brandon Weaver) wrote:
> It's a common enough that the syntax may be justified.

That's just because "map + something" is frequent.  However, blindly adding a "map" feature to anything does not make sense to me.  In fact, "map + select" is much more frequent, but it is not introduced yet (#5663, #15323).  If we add "tally_by" as a shorthand to "map + tally", we should confirm if the combination is truly frequent (i.e., "tally" is rarely used without "map").  We can do it affer only "tally" is released.

----------------------------------------
Feature #11076: Enumerable method count_by
https://bugs.ruby-lang.org/issues/11076#change-76812

* Author: haraldb (Harald Böttiger)
* Status: Closed
* Priority: Normal
* Assignee: nobu (Nobuyoshi Nakada)
* Target version: 
----------------------------------------
I very often use `Hash[array.group_by{|x|x}.map{|x,y|[x,y.size]}]`.

Would be nice with to have a method called `count_by`:

~~~ruby
array = ['aa', 'aA', 'bb', 'cc']
p array.count_by(&:downcase) #=> {'aa'=>2,'bb'=>1,'cc'=>1}
~~~



-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 29+ messages in thread

* [ruby-core:92526] [Ruby trunk Feature#11076] Enumerable method count_by
       [not found] <redmine.issue-11076.20150419203854@ruby-lang.org>
                   ` (27 preceding siblings ...)
  2019-02-15  0:30 ` [ruby-core:91549] " mame
@ 2019-05-02 14:41 ` jonathan
  28 siblings, 0 replies; 29+ messages in thread
From: jonathan @ 2019-05-02 14:41 UTC (permalink / raw)
  To: ruby-core

Issue #11076 has been updated by jonathanhefner (Jonathan Hefner).


> "map + select" is much more frequent, but it is not introduced yet

I think it would also be nice if `filter_map` was added.  However, a specific justification for adding `tally_by` is to avoid an extra array allocation.  `filter_map` can already be expressed as `map { ... }.compact!` to avoid allocating an extra array.  But there is no way to avoid an extra allocation with `map { ... }.tally`.


----------------------------------------
Feature #11076: Enumerable method count_by
https://bugs.ruby-lang.org/issues/11076#change-77888

* Author: haraldb (Harald Böttiger)
* Status: Closed
* Priority: Normal
* Assignee: nobu (Nobuyoshi Nakada)
* Target version: 
----------------------------------------
I very often use `Hash[array.group_by{|x|x}.map{|x,y|[x,y.size]}]`.

Would be nice with to have a method called `count_by`:

~~~ruby
array = ['aa', 'aA', 'bb', 'cc']
p array.count_by(&:downcase) #=> {'aa'=>2,'bb'=>1,'cc'=>1}
~~~



-- 
https://bugs.ruby-lang.org/

Unsubscribe: <mailto:ruby-core-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>

^ permalink raw reply	[flat|nested] 29+ messages in thread

end of thread, other threads:[~2019-05-02 14:46 UTC | newest]

Thread overview: 29+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <redmine.issue-11076.20150419203854@ruby-lang.org>
2015-04-19 20:38 ` [ruby-core:68932] [Ruby trunk - Feature #11076] [Open] Enumerable method count_by bottiger
2015-04-20  1:32 ` [ruby-core:68933] [Ruby trunk - Feature #11076] " shevegen
2015-04-20  6:50 ` [ruby-core:68934] " nobu
2015-04-20  8:14 ` [ruby-core:68935] " duerst
2015-04-20 14:08 ` [ruby-core:68938] " ko1
2015-04-20 15:35 ` [ruby-core:68941] " bottiger
2018-06-29  1:01 ` [ruby-core:87673] [Ruby trunk Feature#11076] " keystonelemur
2018-08-09  7:58 ` [ruby-core:88373] " knu
2018-08-09 18:15 ` [ruby-core:88403] " keystonelemur
2018-08-10 11:37 ` [ruby-core:88436] " janfri26
2018-08-21 17:55 ` [ruby-core:88598] " dgjones
2018-12-13  0:23 ` [ruby-core:90462] " keystonelemur
2018-12-13 10:32 ` [ruby-core:90502] " janfri26
2018-12-14 17:35 ` [ruby-core:90529] " oliverp
2019-01-24  4:22 ` [ruby-core:91244] " joshua.goodall
2019-01-24 14:21 ` [ruby-core:91252] " mame
2019-01-25  0:11 ` [ruby-core:91254] " muraken
2019-01-29  1:05 ` [ruby-core:91312] " keystonelemur
2019-01-29  2:52 ` [ruby-core:91314] " nobu
2019-01-29 10:42 ` [ruby-core:91317] " eregontp
2019-02-02  3:01 ` [ruby-core:91380] " sawadatsuyoshi
2019-02-02  6:21 ` [ruby-core:91381] " duerst
2019-02-06 13:04 ` [ruby-core:91429] " mame
2019-02-07  7:47 ` [ruby-core:91460] " matz
2019-02-07  7:51 ` [ruby-core:91462] " mame
2019-02-07  8:10 ` [ruby-core:91465] " mame
2019-02-14 19:36 ` [ruby-core:91548] " keystonelemur
2019-02-15  0:30 ` [ruby-core:91549] " mame
2019-05-02 14:41 ` [ruby-core:92526] " jonathan

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).