Last active
March 1, 2018 18:59
-
-
Save codeOfRobin/99a91403d7f6545ec76bcd600de9c6be to your computer and use it in GitHub Desktop.
Uh, I can't quite come up with a good name for this
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
// | |
// | |
// Created by Robin Malhotra on 29/01/18. | |
// | |
// Special thanks to DaveDelong for suggesting this: https://github.com/davedelong/Syzygy/blob/master/SyzygyCore/Properties/Property%2BCollections.swift#L76 | |
import RxSwift | |
import Dwifft | |
extension Observable where Element: Collection, Element.Element: Equatable { | |
//implementation taken from https://github.com/RxSwiftCommunity/RxSwiftExt/blob/master/Source/RxSwift/nwise.swift | |
public func nwise(_ n: Int) -> Observable<[E]> { | |
assert(n > 1, "n must be greater than 1") | |
return self | |
.scan([]) { acc, item in Array((acc + [item]).suffix(n)) } | |
.filter { $0.count == n } | |
} | |
//implementations taken from https://github.com/RxSwiftCommunity/RxSwiftExt/blob/master/Source/RxSwift/nwise.swift | |
public func pairwise() -> Observable<(E, E)> { | |
return self.nwise(2) | |
.map { ($0[0], $0[1]) } | |
} | |
public func arrayfied() -> Observable<Array<Element.Element>> { | |
return self.map({ (collection) in | |
return Array(collection) | |
}) | |
} | |
func pairwiseDiffs() -> Observable<[DiffStep<Element.Element>]> { | |
return self.arrayfied().pairwise().map { (oldArray, newArray) in | |
return Dwifft.diff(oldArray, newArray) | |
} | |
} | |
//TODO: Make a generic version that takes a closure (Array, Array) -> (IndexPaths to insert, IndexPaths to delete) that'll in theory work with any diffing library for true decoupling 😃 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment