Skip to content

Instantly share code, notes, and snippets.

@AdamSaleh
Created May 31, 2013 14:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AdamSaleh/5685224 to your computer and use it in GitHub Desktop.
Save AdamSaleh/5685224 to your computer and use it in GitHub Desktop.
Macro-ing contracts
;lets say I have a constrained function like this:
(def constr-sqr
(contracts/with-constraints
sqr
(contracts/contract sqr-o "the only odd"
[n] [odd?])
(contracts/contract sqr-c "the constraining of sqr"
[n] [number? (not= 0 n) => pos? number?])
(contracts/contract sqr-s "only positive"
[n] [pos?])))
; and I'd like to write a macro, that generates the contracts
(def constr-sqr
(contracts/with-constraints
sqr
(my-awesome-contract-macro )
)
; but I can get macro outut only a single form ...
; and for example \w do
(def constr-sqr
(contracts/with-constraints
sqr
(do
(contracts/contract sqr-o "the only odd"
[n] [odd?])
(contracts/contract sqr-c "the constraining of sqr"
[n] [number? (not= 0 n) => pos? number?])
(contracts/contract sqr-s "only positive"
[n] [pos?]))))
; constraints notice only the last contract
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment