[ruby-core:92526] [Ruby trunk Feature#11076] Enumerable method count_by

From: jonathan@...
Date: 2019-05-02 14:41:13 UTC
List: ruby-core #92526
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tiger)
* 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>

In This Thread

Prev Next