Created
October 6, 2016 16:08
-
-
Save awkay/20c3121de91a60aee63a4c71295c270f to your computer and use it in GitHub Desktop.
This file contains 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
(ns sample.macro-fun) | |
#?(:clj | |
(defn some-xform [body] (reverse body))) | |
#?(:clj | |
(defn bump-numbers [body] | |
(map #(if (number? %) (+ 1 %) %) body))) | |
#?(:clj | |
(defn wrap-with [body symbol] | |
(list symbol body))) | |
#?(:cljs | |
(defn render [a] (str "Rendering:" a))) | |
#?(:clj | |
(defmacro abc [nm-sym things-to-do main-body] | |
(let [new-body (reduce (fn [b f] | |
(if (list? f) | |
(let [sym (first f) | |
extra-args (rest f) | |
resolved-fn (resolve sym)] | |
(if resolved-fn | |
(apply resolved-fn b extra-args) | |
b)) | |
(let [resolved-fn (resolve f)] | |
(if resolved-fn | |
(resolved-fn b) | |
b)))) main-body things-to-do)] | |
`(cljs.core/defn ~nm-sym [] ~new-body)))) | |
#?(:cljs | |
(sample.macro-fun/abc result [sample.macro-fun/bump-numbers sample.macro-fun/some-xform (sample.macro-fun/wrap-with render)] (1 2 3 4 +))) | |
(comment | |
(macroexpand-1 '(sample.macro-fun/abc result [sample.macro-fun/bump-numbers sample.macro-fun/some-xform (sample.macro-fun/wrap-with render)] (1 2 3 4 +))) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment