Skip to content

Provide method for sum that accepts a neutral element #18423

Closed
@eschnett

Description

@eschnett

Currently, the expression sum([]) leads to an error, since there is no way to know the intended neutral element ("zero") for an array of type Array{Any}. I suggest to add a method to sum (or to provide a new function sum0) that accepts such a zero element. This would correspond to the existing method reduce(op, v0, itr).

The implementation would be straightforward:

function sum0(itr, v0)
    isempty(itr) && return v0
    sum(itr)
end

Equivalent methods should be added for prod, minimum, and maximum as well.

Whether we can use the name sum for this, or need to use a new name sum0, depends on what methods currently exist for sum and whether there would be a conflict.

Metadata

Metadata

Assignees

No one assigned

    Labels

    docsThis change adds or pertains to documentation

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions