-
-
Save goodmike/425553 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
(defconstrainedfn sqr | |
"Calculates the square of a number." | |
([n] | |
:requires | |
"argument must be a number" | |
(number? n) | |
"argument cannot be zero" | |
(not (zero? n)) | |
:ensures | |
"must evaluate to a positive number" | |
(pos? %) | |
:body | |
(* n n))) | |
(sqr 6) | |
;=> 36 | |
(sqr -6) | |
;=> 36 | |
(sqr 0) | |
;=> java.lang.AssertionError: Assert failed: argument cannot be zero | |
(defconstrainedfn positive-floats | |
"Converts a number to a float. Two numbers to a vector of floats." | |
([x] | |
:requires | |
"argument must be a number" | |
(number? x) | |
"argument must be positive" | |
(pos? x) | |
:ensures | |
(float? %) | |
:body | |
(float x)) | |
([x y] | |
:requires | |
(every? number? [x y]) | |
(every? pos? [x y]) | |
:ensures | |
(every? float? %) | |
:body | |
(str "this is a noop, meant to ensure that multi-expr bodies work") | |
[(float x) (float y)])) | |
(positive-floats 1) | |
;=> 1.0 | |
(positive-floats 1 2) | |
;=> [1.0 2.0] | |
(positive-floats -1) | |
; java.lang.AssertionError: Assert failed: argument must be positive | |
; if no doc string, show condition body | |
(positive-floats 1 -2) | |
; java.lang.AssertionError: Assert failed: (every? pos? [x y]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment