Created
July 12, 2022 22:26
-
-
Save IanKeen/f886eb3f8ebf9b62b2eb9ce609390188 to your computer and use it in GitHub Desktop.
Combine: WithPrevious
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension Publisher { | |
public typealias Pair<T> = (previous: T?, current: T) | |
public func withPrevious() -> AnyPublisher<Pair<Output>, Failure> { | |
return scan(nil) { previous, current -> Pair<Output>? in | |
return Pair(previous: previous?.current, current: current) | |
} | |
.compactMap { $0 } | |
.eraseToAnyPublisher() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment