Skip to content

Instantly share code, notes, and snippets.

@deque-blog
Created January 22, 2017 15:20
Show Gist options
  • Save deque-blog/960eef255f9e73eb9527fd6f1ce0925f to your computer and use it in GitHub Desktop.
Save deque-blog/960eef255f9e73eb9527fd6f1ce0925f to your computer and use it in GitHub Desktop.
(defn- make-op
[rator rands]
(if (< 1 (count rands))
(into [rator] rands)
(first rands)))
(defn- optimize-op
[[rator & rands] binary-op neutral]
(let [{csts true vars false} (group-by cst? rands)
sum-cst (reduce binary-op neutral csts)]
(cond
(empty? vars) (cst sum-cst)
(= neutral sum-cst) (make-op rator vars)
:else (make-op rator (into [(cst sum-cst)] vars))
)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment