# seq.reduce(\_:\_:)

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

1. [Swift](https://developer.apple.com/documentation/swift) ⟩ [Sequence](https://developer.apple.com/documentation/swift/sequence) ⟩
   * [.reduce()](https://developer.apple.com/documentation/swift/sequence/2907677-reduce)
   * [.reduce(into:\_:)](https://developer.apple.com/documentation/swift/sequence/3128812-reduce)
     {% endtab %}

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

1. Advanced Swift, p.37
   {% endtab %}

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

1. [seq.reduce-into-\_](https://lochiwei.gitbook.io/ios/swift/collections/sequence/seq.reduce-into-_ "mention")
2. [reduce-vs-reduceinto](https://lochiwei.gitbook.io/ios/swift/collections/sequence/reduce-vs-reduceinto "mention")
3. [seq.statemap-\_-\_](https://lochiwei.gitbook.io/ios/swift/collections/sequence/seq.statemap-_-_ "mention")
   {% endtab %}
   {% endtabs %}

```swift
// declaration
func reduce<Result>(
    _ initialResult    : Result, 
    // ⭐️ combine function (with return value)
    _ nextPartialResult: (Result, Element) throws -> Result
) rethrows -> Result
```

![](https://1830103165-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M5-JmwCZMKh_d7RfBaN%2Fuploads%2FgjPjNIHLRFm2Is4NdSpb%2Freduce.png?alt=media\&token=61a8a3bf-b71b-4991-8717-5890aa393e19)

{% hint style="info" %}

* 只要設定一個**初始值** (<mark style="color:purple;">**initialResult**</mark>)，此方法就會使用 <mark style="color:purple;">**nextPartialResult**</mark> 這個 **closure** 來進行累算，並將**最後的累算結果**回傳。:point\_right: 比較：[seq.reduce-into-\_](https://lochiwei.gitbook.io/ios/swift/collections/sequence/seq.reduce-into-_ "mention")
  {% endhint %}

{% hint style="danger" %}
on <mark style="color:red;">**every execution**</mark> of the <mark style="color:red;">**combine function**</mark>, a <mark style="color:red;">**brand-new array**</mark> is being **created** by appending the transformed or included element to the previous one. This means that both these implementations are <mark style="color:purple;">**O(n²)**</mark>, not O(n) ...

👉 Advanced Swift, p.37
{% endhint %}

{% hint style="success" %}
[.reduce(into:\_:)](https://developer.apple.com/documentation/swift/sequence/3128812-reduce) is <mark style="color:red;">**preferred**</mark> over [`reduce(_:_:)`](https://lochiwei.gitbook.io/ios/swift/collections/sequence/seq.reduce-_-_) for <mark style="color:red;">**efficiency**</mark> when the <mark style="color:red;">**result**</mark> is a <mark style="color:red;">**copy-on-write**</mark> type, for example an <mark style="color:red;">**Array**</mark> or a <mark style="color:red;">**Dictionary**</mark>.

👉 [.reduce(into:\_:)](https://developer.apple.com/documentation/swift/sequence/3128812-reduce)
{% endhint %}

{% tabs %}
{% tab title="1" %}

```swift
let numbers = [1, 2, 3, 4]
let sum = numbers.reduce(0, +)        // 10
```

{% endtab %}

{% tab title="Second Tab" %}

{% endtab %}
{% endtabs %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://lochiwei.gitbook.io/ios/swift/collections/sequence/seq.reduce-_-_.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
