weavejester / medley

A lightweight library of useful Clojure functions
Eclipse Public License 1.0
865 stars 66 forks source link

Suggestion: template #70

Closed borkdude closed 1 year ago

borkdude commented 1 year ago

I sometimes find myself needing to template EDN literals in code where clojure.template/do-template is too limited (e.g. it doesn't resolve locals) and syntax-quote does too much (it auto-qualifies symbols.

So I wrote this function:

(defn template [expr replacement-map]
  (walk/postwalk
   (fn [expr]
     (if-let [[_ v] (find replacement-map expr)]
       v expr)) expr))

(template '{:linters {:unresolved-symbol {:exclude [::foo y z]}}} '{::foo x})
;;=> {:linters {:unresolved-symbol {:exclude [x y z]}}}

Perhaps there are more people who regularly need this templating function. It's too small to live in a library but perhaps it would be good to have this in medley.

weavejester commented 1 year ago

My initial reaction to this is that it might be a little too specialized for Medley.

borkdude commented 1 year ago

No problem. I'll close and we can re-open if in the future there's more general interest.

borkdude commented 1 year ago

Guess what: this is effectively walk/postwalk-replace but with the arguments swapped :)