Created
June 20, 2011 13:19
-
-
Save odyssomay/1035590 to your computer and use it in GitHub Desktop.
Macro to create a callable record with any arity
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
(defmacro definvokerecord [invoke_fn & body] | |
`(defrecord | |
~@body | |
clojure.lang.IFn | |
~@(map (fn [n] | |
(let [args (for [i (range n)] (symbol (str "arg" i)))] | |
(if (empty? args) | |
`(~'invoke [this#] | |
(~invoke_fn this#)) | |
`(~'invoke [this# ~@args] | |
(~invoke_fn this# ~@args))))) (range 21)) | |
(~'applyTo [this# args#] | |
(apply ~invoke_fn this# args#)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Good suggestion, I changed it.