Created
June 2, 2016 13:29
-
-
Save serialhex/e8273aedfa87a08965627fa9f49e5606 to your computer and use it in GitHub Desktop.
Simple Semantic Versioning Tests for Common Lisp
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
;; This function is the only one that needs to be defined, | |
;; everything else can be defined in terms of this one function. | |
;; YAY LOGIC! | |
;; | |
;; This assumes only *simple* semvers containing only *numbers* | |
;; so something like "1.3.9-beta" it will barf on... | |
;; maybe something for an extension later? | |
;; | |
;; run it like this: | |
;; `(semver<= "1.2.3" "1.2.3")` | |
;; => T | |
;; `(semver<= "1.2.29" "1.2.3")` | |
;; => NIL | |
;; the rest work as you would expect them to! | |
(defun semver<= (a b) | |
(let ((f (mapcar #'parse-integer (cl-ppcre:split "\\." a))) | |
(s (mapcar #'parse-integer (cl-ppcre:split "\\." b)))) | |
(labels ((rec-test (n-a n-b) | |
(let ((fst-a (car n-a)) | |
(fst-b (car n-b))) | |
(cond | |
((or (null fst-a) (null fst-b)) (and (null fst-a) (null fst-b))) | |
((= fst-a fst-b) (rec-test (cdr n-a) (cdr n-b))) | |
(t (< fst-a fst-b)))))) | |
(rec-test f s)))) | |
(defun semver= (a b) | |
(and (semver<= a b) (semver<= b a))) | |
(defun semver/= (a b) | |
(not (semver= a b))) | |
(defun semver< (a b) | |
(and (semver<= a b) (not (semver<= b a)))) | |
(defun semver>= (a b) | |
(semver<= b a)) | |
(defun semver> (a b) | |
(semver< b a)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment