Created
April 16, 2013 21:02
-
-
Save timmc/5399621 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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