Skip to content

Instantly share code, notes, and snippets.

@selfsame
Created March 16, 2015 22:43
Show Gist options
  • Select an option

  • Save selfsame/0d2b496fb24343143e7a to your computer and use it in GitHub Desktop.

Select an option

Save selfsame/0d2b496fb24343143e7a to your computer and use it in GitHub Desktop.
infix macro
;Infix macro
(def ^:private ordered-ops ['* '/ '+ '-])
(defn ^:private non-op? [x] (if ((set ordered-ops) x) false true))
(defn ^:private num-or-seq? [e] (or (number? e) (sequential? e)))
(defn ^:private group [col op]
(let [pass1 (partition-by #(or (non-op? %) (= op %)) col)]
(map
#(if-not (> (count %) 1)
(first %)
(let [de-opped (filter non-op? %)]
(if (not= (count de-opped) (count %))
(cons op (filter non-op? %))
%)))
pass1)))
(defn ^:private red-inf [col]
(let [col-2 (map #(if (and (list? %) (not= 1 (count %))) (red-inf %) %) col)]
(first (reduce group col-2 ordered-ops))))
(defmacro $ [& more]
(let [transformed (red-inf more)]
`(~@transformed)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment