Skip to content

Instantly share code, notes, and snippets.

@pervognsen
Created March 13, 2010 09:00
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 pervognsen/331211 to your computer and use it in GitHub Desktop.
Save pervognsen/331211 to your computer and use it in GitHub Desktop.
(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