seq.sum

available when Element conforms to AdditiveArithmetic.

๐Ÿ’พ ็จ‹ๅผ๏ผš paiza.io

// ๐Ÿ…ฟ๏ธ `AdditiveArithmetic`: supports +, -, .zero.
extension Sequence where Element: AdditiveArithmetic {
    /// sum of the elements.
    /// ```
    /// [1, 2, 3, 4, 5].sum       // 15
    /// ```
    public var sum: Element { reduce(.zero, +) }
}

let sum = [1, 2, 3, 4, 5].sum       // 15

Last updated