Skip to content

Instantly share code, notes, and snippets.

@IanKeen
Created July 12, 2022 22:26
Show Gist options
  • Save IanKeen/f886eb3f8ebf9b62b2eb9ce609390188 to your computer and use it in GitHub Desktop.
Save IanKeen/f886eb3f8ebf9b62b2eb9ce609390188 to your computer and use it in GitHub Desktop.
Combine: WithPrevious
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