From: sawadatsuyoshi@... Date: 2021-02-15T06:32:15+00:00 Subject: [ruby-core:102494] [Ruby master Feature#17608] Compact and sum in one step Issue #17608 has been updated by sawa (Tsuyoshi Sawada). ko1 (Koichi Sasada) wrote in #note-11: > Can you count on your app or your observation? > > ``` > ko1@aluminium:~$ gem-codesearch 'compact\.sum' | wc -l > 75 > ``` > > not so many cases in gem-codesearch. I searched on my company's private repository (the `***` in the following was replaced by the repository name): https://github.com/search?q=org%3A**********+compact.sum&type=Code and got 94 results. The search itself may match not only exactly `compact.sum` but also strings like `compact_sum`, so I checked through manually, and all of them were `compact.sum` or a variant of it, like `&.compact&.sum`. When I switch to all results on GitHub: https://github.com/search?q=compact.sum&type=Code it says 70,041, but this time, it includes strings like `compact_sum`. I am not sure how many of them are genuine `compact.sum`, but I believe there are many. You presented the result on gems, but my guess is that `sum` is a relatively newly introduced method, and so gems tend to avoid `sum` in the first place to make them compatible with old Rubies, hence, gems are biased as data source for this purpose. And please don't forget that, besides `compact.sum`, code fragments like `sum{_1 || 0}` are also relevant use cases. ---------------------------------------- Feature #17608: Compact and sum in one step https://bugs.ruby-lang.org/issues/17608#change-90388 * Author: sawa (Tsuyoshi Sawada) * Status: Rejected * Priority: Normal ---------------------------------------- Many use cases of `Array#sum` are preceded with the `compact` method or are followed by a block to ensure the value is addable. ```ruby a = [1, nil, 2, 3] a.sum # !> TypeError a.compact.sum # => 6 a.sum{_1 || 0} # => 6 ``` I propose there should be a way to do that in one step. I request either of the following: A. Change the current behaviour to skip `nil`s. ```ruby a.sum # => 6 ``` B. `Array#filter_sum` method ```ruby a.filter_sum # => 6 ``` C. An option for `Array#sum` ```ruby a.sum(compact: true) # => 6 ``` -- https://bugs.ruby-lang.org/ Unsubscribe: