Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- extension Collection where Element: Hashable, Index == Int {
- func changes(comparedTo other: Self) -> [CollectionChange] {
- var oldDict = Dictionary(uniqueKeysWithValues: zip(other, other.startIndex..<other.endIndex))
- var changes = enumerated().map { (newValue, element) -> CollectionChange in
- if let oldValue = oldDict.removeValue(forKey: element) {
- return .move(from: oldValue, to: newValue)
- } else {
- return .insert(at: newValue)
- }
- }
- changes += oldDict.map { .remove(at: $0.value) }
- return changes
- }
- }
- enum CollectionChange {
- case insert(at: Int)
- case remove(at: Int)
- case move(from: Int, to: Int)
- }
Add Comment
Please, Sign In to add comment