Generic Instance Method

scan(_:_:)

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

Declaration

func scan<T>(_ initialResult: T, _ nextPartialResult: @escaping (T, Upstream.Output) -> T) -> Publishers.Scan<Publishers.Share<Upstream>, 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

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

Beta Software

This documentation contains preliminary information about an API or technology in development. This information is subject to change, and software implemented according to this documentation should be tested with final operating system software.

Learn more about using Apple's beta software