Skip to content

Instantly share code, notes, and snippets.

@brandonbloom
Created November 22, 2012 07: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 brandonbloom/4129774 to your computer and use it in GitHub Desktop.
Save brandonbloom/4129774 to your computer and use it in GitHub Desktop.
(defn- unquote? [form]
(and (seq? form) (= (first form) 'clojure.core/unquote)))
(defn- unquote-splicing? [form]
(and (seq? form) (= (first form) 'clojure.core/unquote-splicing)))
(defn syntax-quote-fn [form]
(cond
(unquote? form) (second form)
(unquote-splicing? form) (throw (Exception. "splice not in list"))
(coll? form)
(let [xs (if (map? form) (apply concat form) form)
cat `(concat ~@(for [x (partition-by unquote-splicing? xs)]
(if (unquote-splicing? (first x))
(second (first x))
`[~@(map syntax-quote-fn x)])))]
(cond
(vector? form) `(vec ~cat)
(map? form) `(apply hash-map ~cat)
(set? form) `(set ~cat)
(seq? form) `(list* ~cat)
:else (throw (Exception. "Unknown collection type"))))
:else `'~form))
(defmacro syntax-quote [form]
`(syntax-quote-fn '~form))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment