Skip to content

Instantly share code, notes, and snippets.

@timmc
Created April 16, 2013 21:02
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 timmc/5399621 to your computer and use it in GitHub Desktop.
Save timmc/5399621 to your computer and use it in GitHub Desktop.
(ns adhoc.core
(:require clojure.walk))
(defn prewalk-splice
[smap form]
(if-let [seq-reshaper (cond (seq? form) identity
(list? form) (partial apply list) ;; necessary?
;; map entries look like vectors
(instance? java.util.Map$Entry form) nil
(vector? form) vec
(set? form) set)]
;; we know how to splice into this form
(seq-reshaper
(mapcat (fn [sub] (if (contains? smap sub)
(smap sub)
[(prewalk-splice smap sub)]))
form))
;; otherwise let walk descend into it
(clojure.walk/walk (partial prewalk-splice smap) identity form)))
(comment
(prewalk-splice '{a [1 2 3] b [4 5 6]} '((. a ..) {#{:a b :C} [- b +]}))
#_((. 1 2 3 ..) {#{:C 4 5 6 :a} [- 4 5 6 +]}))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment