Guest User

Untitled

a guest
Feb 20th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.72 KB | None | 0 0
  1. extension Collection where Element: Hashable, Index == Int {
  2.  
  3. func changes(comparedTo other: Self) -> [CollectionChange] {
  4. var oldDict = Dictionary(uniqueKeysWithValues: zip(other, other.startIndex..<other.endIndex))
  5. var changes = enumerated().map { (newValue, element) -> CollectionChange in
  6. if let oldValue = oldDict.removeValue(forKey: element) {
  7. return .move(from: oldValue, to: newValue)
  8. } else {
  9. return .insert(at: newValue)
  10. }
  11. }
  12. changes += oldDict.map { .remove(at: $0.value) }
  13. return changes
  14. }
  15. }
  16.  
  17. enum CollectionChange {
  18. case insert(at: Int)
  19. case remove(at: Int)
  20. case move(from: Int, to: Int)
  21. }
Add Comment
Please, Sign In to add comment