Sequence

public extension Sequence where Self.Element: AdditiveArithmetic

Available where Self.Element: AdditiveArithmetic

  • Returns the sum of all elements in the sequence.

    The following examples show how to use sum on different sequences.

    let arraySum = [1.1, 2.2, 3.3, 4.4, 5.5].sum()
    // arraySum == 16.5
    
    let rangeSum = (1..<10).sum()
    // rangeSum == 45
    
    let setSum = Set(arrayLiteral: 1, 2, 3, 2, 3).sum()
    // setSum == 6
    

    Declaration

    Swift

    func sum() -> Element

    Return Value

    The total of the elements. If the sequence has no elements, returns zero.