Project

General

Profile

Actions

Feature #17608

closed

Compact and sum in one step

Added by sawa (Tsuyoshi Sawada) almost 4 years ago. Updated over 3 years ago.

Status:
Rejected
Assignee:
-
Target version:
-
[ruby-core:102393]

Description

Many use cases of Array#sum are preceded with the compact method or are followed by a block to ensure the value is addable.

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 nils.

a.sum # => 6

B. Array#filter_sum method

a.filter_sum # => 6

C. An option for Array#sum

a.sum(compact: true) # => 6
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0