ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
From: mame@ruby-lang.org
To: ruby-core@ruby-lang.org
Subject: [ruby-core:91252] [Ruby trunk Feature#11076] Enumerable method count_by
Date: Thu, 24 Jan 2019 14:21:12 +0000 (UTC)	[thread overview]
Message-ID: <redmine.journal-76498.20190124142109.c9431b7054370efd@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-11076.20150419203854@ruby-lang.org

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/

  parent reply	other threads:[~2019-01-24 14:21 UTC|newest]

Thread overview: 29+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [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 ` mame [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-list from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.ruby-lang.org/en/community/mailing-lists/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=redmine.journal-76498.20190124142109.c9431b7054370efd@ruby-lang.org \
    --to=ruby-core@ruby-lang.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).