From: mame@ruby-lang.org
To: ruby-core@ruby-lang.org
Subject: [ruby-core:91429] [Ruby trunk Feature#11076] Enumerable method count_by
Date: Wed, 06 Feb 2019 13:04:52 +0000 (UTC) [thread overview]
Message-ID: <redmine.journal-76688.20190206130450.61f38ae3c4aaff6e@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-11076.20150419203854@ruby-lang.org
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/
next prev parent reply other threads:[~2019-02-06 13:05 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 ` [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 ` mame [this message]
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-76688.20190206130450.61f38ae3c4aaff6e@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).