Skip to content

Instantly share code, notes, and snippets.

@brianfay
Created January 20, 2016 04:36
Show Gist options
  • Save brianfay/e71e17b9d0fa01a072e4 to your computer and use it in GitHub Desktop.
Save brianfay/e71e17b9d0fa01a072e4 to your computer and use it in GitHub Desktop.
Nasty WIP thing that will take two sequences and return a list of actions that will transform the first sequence into the second one.
(defn diff-sorted
[old new]
;;iterate through new list
(let [old-set (set old)]
(loop [actions '()
old-list old
new-list new]
(let [old-val (first old-list)
new-val (first new-list)
rest-old (rest old-list)
rest-new (rest new-list)]
(cond
(empty? new-list) actions
(= old-val new-val) (recur (cons "nothing" actions) rest-old rest-new)
(contains? old-set new-val) (recur (cons "move" actions) rest-old rest-new)
:else (recur (cons "add" actions) rest-old rest-new))))))
;;currently does not return delete actions - should be easy if order of actions doesn't matter
(diff-sorted [:a :b :c] [:a :b :c])
(diff-sorted '(1 2 3) '(2 3 4))
(diff-sorted [1 2 3] [2 3 4])
(diff-sorted [1 2 3] [2 3 4 5 6])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment