Created
May 27, 2021 04:14
-
-
Save levantAJ/c93b92566b27e6125f6e8e8607d6b650 to your computer and use it in GitHub Desktop.
Get previous & current value of a Observable
This file contains hidden or 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 ObservableType { | |
func withPrevious() -> Observable<(previous: Element?, current: Element?)> { | |
return scan([]) { (previous, current) in | |
return Array(previous + [current]).suffix(2) | |
} | |
.map { array -> (previous: Element?, current: Element?) in | |
let previous = array.count > 1 ? array.first : nil | |
let current = array.last | |
return (previous, current) | |
} | |
} | |
func withPrevious(startWith first: Element) -> Observable<(Element, Element)> { | |
return scan((first, first)) { ($0.1, $1) }.skip(1) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment