Created
March 16, 2015 22:43
-
-
Save selfsame/0d2b496fb24343143e7a to your computer and use it in GitHub Desktop.
infix macro
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
| ;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