Skip to content

Instantly share code, notes, and snippets.

@uwo
Created November 15, 2018 21:27
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 uwo/9ae7a83737093dd59b6a1c6086cc0837 to your computer and use it in GitHub Desktop.
Save uwo/9ae7a83737093dd59b6a1c6086cc0837 to your computer and use it in GitHub Desktop.
to-pull-expression
(ns pull-util)
(defn to-pull-expression
"Naively build pull expression from given tree. Doesn't work with refs
that are represented by idents (keywords), e.g. enums, at the moment."
[tree]
(letfn [(card-many-ref? [v]
(and (sequential? v)
(every? map? v)))
(branch? [v]
(or (map? v) (card-many-ref? v)))]
(let [{branch-kvps true
leaf-kvps false} (group-by (fn [[_ v]] (branch? v)) tree)
map-spec (into {} (map (fn [[k v]]
(let [sub-exp (if (sequential? v)
(vec (mapcat to-pull-expression v))
(to-pull-expression v))]
[k sub-exp])))
branch-kvps)
leaves (into [] (map first) leaf-kvps)]
(if (seq map-spec)
(conj leaves map-spec)
leaves))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment