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 specif=
ic justification for adding `tally_by` is to avoid an extra array allocatio=
n.  `filter_map` can already be expressed as `map { ... }.compact!` to avoi=
d allocating an extra array.  But there is no way to avoid an extra allocat=
ion with `map { ... }.tally`.


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

* Author: haraldb (Harald B=F6ttiger)
* 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 =3D ['aa', 'aA', 'bb', 'cc']
p array.count_by(&:downcase) #=3D> {'aa'=3D>2,'bb'=3D>1,'cc'=3D>1}
~~~



-- =

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

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