Created
June 4, 2010 15:08
-
-
Save fogus/425520 to your computer and use it in GitHub Desktop.
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
(defconstrainedfn sqr | |
"Calculates the square of a number." | |
([n] | |
:requires | |
(number? n) | |
(not (zero? n)) | |
:ensures | |
(pos? %) | |
:body | |
(* n n))) | |
(sqr 6) | |
;=> 36 | |
(sqr -6) | |
;=> 36 | |
(sqr 0) | |
;=> java.lang.AssertionError: Assert failed: (not (zero? n)) | |
(defconstrainedfn positive-floats | |
"Converts a number to a float. Two numbers to a vector of floats." | |
([x] | |
:requires | |
(number? x) | |
(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: (pos? x) | |
(positive-floats 1 -2) | |
; java.lang.AssertionError: Assert failed: (every? pos? [x y]) |
@samaaron This is a bit of a departure from the current defn, especially with the :body
element. I'm still tweaking the syntax but maybe an eye toward eventual Clojure.core consumption would be a nice idea. ;-)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@goodmike I think a doc string might work, especially when I get around to throwing custom errors. I'm still chewing on this at the moment.