Skip to content

Instantly share code, notes, and snippets.

@sergeyklay
Created June 22, 2019 10:58
Show Gist options
  • Save sergeyklay/7f654976c441929d9d52b928b5eab869 to your computer and use it in GitHub Desktop.
Save sergeyklay/7f654976c441929d9d52b928b5eab869 to your computer and use it in GitHub Desktop.
Simple version compare using clojure
(defn- normalize-verison
[version]
(let [[x y z] (map read-string (clojure.string/split version #"\."))
z (or z 0)]
(+ (* x 10000) (* y 100) z)))
(defn version-compare
[a b]
(let [left (normalize-verison a)
right (normalize-verison b)]
(cond
(< left right) -1
(> left right) 1
:else 0)))
(version-compare "1.3.1" "1.3.0")
;; 1
(version-compare "1.3.1" "1.3.2")
;; -1
(version-compare "1.3.1" "1.3.1")
;; 0
(version-compare "1.10.1" "1.3.0")
;; 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment