Skip to content

Instantly share code, notes, and snippets.

@jdubie
Last active March 5, 2017 12:30
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jdubie/c8b62e24f7072078abc3 to your computer and use it in GitHub Desktop.
Save jdubie/c8b62e24f7072078abc3 to your computer and use it in GitHub Desktop.
ClojureScript macros.
;; nice for reagent
(defmacro defc
"syntactic sugar for declaring stateful components. Keeps signature of inner
and outer params consistent.
Before:
(defn stateful-component
[params]
(let [local state]
(fn [params])
body)))
After:
(defc stateful-component
[params]
[local state]
body)"
[component-name params let-block & body]
(let [inner (concat `(fn ~params) body)]
`(def ~component-name (fn ~params
(let ~let-block ~inner)))))
;; generally useful
(defmacro mirror
"shorthand for declaring a map where the keys are keywords that correspond to
the value names. CoffeeScript users might find this as familiar shorthand.
Before:
{:foo foo :bar bar}
After:
(mirror foo bar)"
[& syms]
(apply hash-map (flatten (map (fn [s] [(keyword s) s]) syms))))
(defmacro defm
"shorthand for defining referentially transparent functions. use in place of
`defn` for referentially transparent functions.
(defm add
[x y]
(+ x Y)"
[fn-name params & body]
(let [inner (concat `(fn ~params) body)]
`(def ~fn-name (memoize ~inner))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment