Skip to content

Instantly share code, notes, and snippets.

@rauhs
Last active December 4, 2017 07:54
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 rauhs/d2575e77e6e063ae94abbd9f1bca226d to your computer and use it in GitHub Desktop.
Save rauhs/d2575e77e6e063ae94abbd9f1bca226d to your computer and use it in GitHub Desktop.
;; FROM: https://github.com/gfredericks/misquote
(defn- listish?
"Gets both lists and lazy seqs."
[ob]
(and (sequential? ob) (not (vector? ob))))
(defn- unquote-form?
[form]
(and (listish? form)
(= 'clojure.core/unquote (first form))))
(defn- unquote-splicing-form?
[form]
(and (listish? form)
(= 'clojure.core/unquote-splicing (first form))))
(def ^{:arglists '([x])} scalar?
(some-fn number? true? false? nil? keyword? symbol? string?))
(defn misquote'
[arg]
(cond
(unquote-form? arg)
(second arg)
(vector? arg)
`(vec ~(misquote' (seq arg)))
(set? arg)
`(set ~(misquote' (seq arg)))
(map? arg)
`(into {} ~(misquote' (map vec arg)))
(listish? arg)
(cons `concat
(for [x arg]
(cond (unquote-form? x)
[(second x)]
(unquote-splicing-form? x)
(second x)
:else
[(misquote' x)])))
(scalar? arg) (list 'quote arg)))
(defmacro misquote
"Like quote but can use the unquote operator (~) for eval.
(misquote '[:foo ~bar])"
[arg]
(misquote' (second arg)))
(def query-pull-pattern
"The query pull pattern to get schema/propt/(post)-display"
[:query/id
{:query/schema [:schema/id :schema/official?]}
{:query/prompt [:schema.field/name]}
{:query/display [:schema.field/name]}
{:query/post-display [:schema.field/name]}])
(mq/misquote
'[:find [(pull ?e ~query-pull-pattern) ...]
:in $ ?sch-id
:where
[?sch :schema/id ?sch-id]
[?e :query/schema ?sch]])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment