Created
August 20, 2010 18:34
-
-
Save fogus/540876 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
(def sqr #(*% %)) | |
(def csqr (with-constraints sqr | |
(contract number-cnstr | |
"constrains the act of squaring" | |
[n] number? (not= 0 n) => number? pos?)) | |
(csqr 10) | |
;=> 100 | |
(csqr -10) | |
;=> 100 | |
(csqr :a) | |
; java.lang.AssertionError: Assert failed: (number? n) | |
(csqr 0) | |
; java.lang.AssertionError: Assert failed: (not= 0 n) |
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
(def sqr #(*% %)) | |
(def csqr (with-constraints sqr | |
(contract number-cnstr | |
"constrains squaring to numbers" | |
[n] number? => number?) | |
(contract pos-cnstr | |
"constrains squaring to positives" | |
[n] (not= 0 n) => pos?))) | |
(csqr 10) | |
;=> 100 | |
(csqr -10) | |
;=> 100 | |
(csqr :a) | |
; java.lang.AssertionError: Assert failed: (number? n) | |
(csqr 0) | |
; java.lang.AssertionError: Assert failed: (not= 0 n) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
See previous version for a different way to do the same thing.