# seq.sum

{% tabs %}
{% tab title="🌀 Sequence" %}
{% hint style="warning" %}
available when <mark style="color:purple;">**Element**</mark> conforms to <mark style="color:red;">**AdditiveArithmetic**</mark>.
{% endhint %}

💾 程式： [paiza.io](https://paiza.io/projects/MP35dRFObjKDgqSB6TElNA?language=swift)

````swift
// 🅿️ `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
````

{% endtab %}

{% tab title="📗 參考" %}

* Jesse ⟩ [Exploring Swift’s numeric types and protocols](https://speakerdeck.com/jessesquires/exploring-swifts-numeric-types-and-protocols?slide=12)
  {% endtab %}

{% tab title="📘 手冊" %}

* SwiftDoc ⟩ [Numeric](https://swiftdoc.org/v5.1/protocol/numeric/)
* Swift ⟩ [Numbers and Basic Values](https://developer.apple.com/documentation/swift/swift_standard_library/numbers_and_basic_values) ⟩ [Numeric Protocols](https://developer.apple.com/documentation/swift/swift_standard_library/numbers_and_basic_values/numeric_protocols) ⟩ [Numeric](https://developer.apple.com/documentation/swift/numeric)
  {% endtab %}

{% tab title="👥 相關" %}

* [](https://lochiwei.gitbook.io/ios/swift/collections/sequence "mention") extension (<mark style="color:red;">**where**</mark> Element: [**AdditiveArithmetic**](https://lochiwei.gitbook.io/ios/swift/type/category/basic/numbers)) .
  {% endtab %}
  {% endtabs %}
