Skip to content

Instantly share code, notes, and snippets.

@awkay
Created October 6, 2016 16:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save awkay/20c3121de91a60aee63a4c71295c270f to your computer and use it in GitHub Desktop.
Save awkay/20c3121de91a60aee63a4c71295c270f to your computer and use it in GitHub Desktop.
(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