From: keystonelemur@... Date: 2018-06-29T01:01:03+00:00 Subject: [ruby-core:87673] [Ruby trunk Feature#11076] Enumerable method count_by Issue #11076 has been updated by baweaver (Brandon Weaver). Has there been any thought on this as a language feature? There was an earlier conversation demonstrating a practical use for this feature, and I had mentioned a few of the core maintainers to bring the subject back into consideration: https://twitter.com/keystonelemur/status/1012434696909852672 nobu had recently updated his patch here: https://github.com/ruby/ruby/compare/trunk...nobu:feature/11076-Enumerable%23count_by I still believe this would be an incredibly useful feature to have in the core of the language, as a very common pattern to work around it is unintuitive for newer programmers: ``` # Most common array .group_by { |v| v } .map { |k, v| [k, v.size] } .to_h # In older versions: Hash[array.group_by { |v| v }.map { |k, v| [k, v.size] }] # or in more recent versions: array .group_by { |v| v } .transform_values(&:size) # or using reduce / ewo: array.each_with_object(Hash.new(0)) { |v, h| h[v] += 1 } ``` By giving a name to this concept, we've made it more accessible as well. Given the current trend of 2.6, I believe this would be a welcome addition. ---------------------------------------- Feature #11076: Enumerable method count_by https://bugs.ruby-lang.org/issues/11076#change-72696 * 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/ Unsubscribe: