Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
(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