Skip to content

Instantly share code, notes, and snippets.

@odyssomay
Created June 20, 2011 13:19
Show Gist options
  • Save odyssomay/1035590 to your computer and use it in GitHub Desktop.
Save odyssomay/1035590 to your computer and use it in GitHub Desktop.
Macro to create a callable record with any arity
(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#))))
@miner
Copy link

miner commented Jun 20, 2011

It looks like args could be simplified to (for [i (range n)] (symbol (str "arg" i)))

@odyssomay
Copy link
Author

Good suggestion, I changed it.

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