ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
From: bottiger@live.com
To: ruby-core@ruby-lang.org
Subject: [ruby-core:68941] [Ruby trunk - Feature #11076] Enumerable method count_by
Date: Mon, 20 Apr 2015 15:35:27 +0000	[thread overview]
Message-ID: <redmine.journal-52204.20150420153526.2e765f62252044ab@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-11076.20150419203854@ruby-lang.org

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/

  parent reply	other threads:[~2015-04-20 15:23 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 ` bottiger [this message]
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

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-52204.20150420153526.2e765f62252044ab@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).