Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace nil with explicit sentinel for diff and patch #8

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/clj/clj_diff/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@

(defn patch*
[s edit-script]
(filter #(not (nil? %)) (merge-patch s edit-script nil)))
(filter #(not (= ::delete-sentinel %)) (merge-patch s edit-script ::delete-sentinel)))

(defmulti ^{:arglists '([s edit-script])} patch
"Use the instructions in the edit script to transform the sequence s into
Expand Down
4 changes: 2 additions & 2 deletions src/clj/clj_diff/miller.clj
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@
(let [d (edit-dist delta p k)
head-x (backtrack-snake a b x (- x k))]
(loop [head-x head-x]
(let [move (first (filter #(and (not (nil? %)) ;; <<<===
(let [move (first (filter #(and (not (= ::sentinel %)) ;; <<<===
(= (:d %) (dec d)))
(map #(% graph delta p head-x k)
[look-left look-up])))]
Expand Down Expand Up @@ -195,7 +195,7 @@
edits))

(defn vectorize [& more]
(map #(vec (cons nil %)) more))
(map #(vec (cons ::sentinel %)) more))

(defn order->ses
[a b]
Expand Down
8 changes: 8 additions & 0 deletions test/clj_diff/test/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@
(let [t (fn [a b] (edit-distance (diff a b)))]
(is (= (t [1 2 3 4 3 2 3 2 1 2 3] [2 3 1 2 3 4 5 4 3])
10))
(is (= (t [nil 1 2] [1 2])
1))
(is (= (t [1 2] [nil 1 2])
1))
(is (= (t [nil 1 2] [nil 1 2])
0))
(is (= (t "abcab" "cbab")
3))
(is (= (t "abcabba" "cbabac")
Expand Down Expand Up @@ -69,6 +75,8 @@
(are [a b]
(= b (patch a (diff a b)))

[nil 42] [42]
[42] [42 nil]
"aba" "aca"
"abcabba" "cbabac"
"FWdRgevf43" "T5C7U3Lz5v"
Expand Down