Skip to content

Instantly share code, notes, and snippets.

@fogus
Created August 20, 2010 18:34
Show Gist options
  • Save fogus/540876 to your computer and use it in GitHub Desktop.
Save fogus/540876 to your computer and use it in GitHub Desktop.
(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)
(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)
@fogus
Copy link
Author

fogus commented Aug 20, 2010

See previous version for a different way to do the same thing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment