<!--
{
  "availability" : [
    "iOS: 13.0.0 -",
    "iPadOS: 13.0.0 -",
    "macCatalyst: 13.0.0 -",
    "macOS: 10.15.0 -",
    "tvOS: 13.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 6.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Combine",
  "identifier" : "/documentation/Combine/Publisher/scan(_:_:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Combine"
    ],
    "preciseIdentifier" : "s:7Combine9PublisherPAAE4scanyAA10PublishersO4ScanVy_xqd__Gqd___qd__qd___6OutputQztctlF"
  },
  "title" : "scan(_:_:)"
}
-->

# scan(_:_:)

Transforms elements from the upstream publisher by providing the current
element to a closure along with the last value returned by the closure.

```
func scan<T>(_ initialResult: T, _ nextPartialResult: @escaping (T, Self.Output) -> T) -> Publishers.Scan<Self, T>
```

## Parameters

`initialResult`

The previous result returned by the `nextPartialResult` closure.

`nextPartialResult`

A closure that takes as its arguments the previous value returned by the closure and the next element emitted from the upstream publisher.

## Return Value

A publisher that transforms elements by applying a closure that receives its previous return value and the next element from the upstream publisher.

## Discussion

Use [`scan(_:_:)`](/documentation/Combine/Publisher/scan(_:_:)) to accumulate all previously-published values into a single
value, which you then combine with each newly-published value.

The following example logs a running total of all values received
from the sequence publisher.

```
let range = (0...5)
cancellable = range.publisher
    .scan(0) { return $0 + $1 }
    .sink { print ("\($0)", terminator: " ") }
 // Prints: "0 1 3 6 10 15 ".
```

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)