Skip to content

Instantly share code, notes, and snippets.

@Jannis
Created January 18, 2017 12:35
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 Jannis/af5c0dfed612fae19cb7b38705ad561c to your computer and use it in GitHub Desktop.
Save Jannis/af5c0dfed612fae19cb7b38705ad561c to your computer and use it in GitHub Desktop.
;; Query: A vector of keywords (e.g. [:foo :bar])
(defmacro with-query-bindings
"Binds names derived from a query to the results for the query.
E.g. the query [:foo :bar] is turned into
(let [{foo :foo bar :bar} <query result>]
<body>)"
[query query-result & body]
(let [bindings (query-bindings query)]
`(let [~bindings ~query-result]
~@body)))
(defmacro foo
[query query-result & body]
`(with-query-bindings ~query ~query-result
~@body))
;; Works fine
(foo
[:foo :bar] ; Query
{:foo "VALUE OF FOO" :bar "VALUE OF BAR"} ; Query result
(println foo) ; Body
(println bar))
;; I'd like this to work as well:
(defn make-query []
[:foo :bar])
(foo
(make-query) ; Query generating expression
{:foo "VALUE OF FOO" :bar "VALUE OF BAR"} ; Query result
(println foo) ; Body
(println bar))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment