Created
June 22, 2019 10:58
-
-
Save sergeyklay/7f654976c441929d9d52b928b5eab869 to your computer and use it in GitHub Desktop.
Simple version compare using clojure
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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