Skip to content

Instantly share code, notes, and snippets.

@miikka

miikka/core.clj Secret

Created August 13, 2016 07:51
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 miikka/ed0106b8b8a99a710cd2dd65e02b0b34 to your computer and use it in GitHub Desktop.
Save miikka/ed0106b8b8a99a710cd2dd65e02b0b34 to your computer and use it in GitHub Desktop.
(ns foo.core)
(defmacro
fn+
"Destructure params marked with ^js as JavaScript objects."
[args & body]
(let [js-map (into {} (for [x args :when (= (:tag (meta x)) 'js)]
[x (gensym)]))
js-args (into [] (for [x args] (get js-map x x)))
dst (into [] (apply concat (for [[x n] js-map] `[~x (js->clj ~n)])))]
`(fn ~js-args (let ~dst ~@body))))
;; => (def a-plus-b (fn+ [^js {:strs [a b]}] (+ a b)))
;; #'cljs.user/a-plus-b
;; => (a-plus-b #js {:a 1 :b 2})
;; 3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment