Created
March 13, 2010 09:00
-
-
Save pervognsen/331211 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 *max-unroll-arity* 8) ; should probably be a defbinop keyword argument instead | |
(defmacro defbinop [name & fdecl] | |
(let [var-supply (repeatedly gensym)] | |
`(defn ~name | |
~@fdecl | |
~@(for [n (range 3 *max-unroll-arity*)] | |
(let [vars (take n var-supply)] | |
`([~@vars] ~(reduce (fn [x y] `(~name ~x ~y)) vars)))) | |
~(let [vars (take *max-unroll-arity* var-supply)] | |
`([~@vars & ~'more] | |
(reduce ~name (concat (list ~@vars) ~'more))))))) | |
;; example | |
(binding [*max-unroll-arity* 16] ;; double the default | |
(defbinop my-add | |
([] (+)) | |
([x] (+ x)) | |
([x y] (+ x y)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment