Hello ๐
How do I mimic "clojure.core/filter" & first with rewrite-clj? Searched the docs but couldn't find the answer.
Context: I have an edn file that contains a vector and I want to replace the entry that contains a specific id.
I'm pretty sure I can use rewrite-clj.zip/replace as soon as I identify the correct node but I'm having trouble finding it.
Example in ๐งต
z/sexpr may be useful to you in the future, rather than comparing strings.
z/sexpr may blow up in your face sometimes though ;)
I'll look it up, thanks
(def content "[{:id :a :label \"foo\"}
{:id :b :label \"bar\"}
{:id :c :label \"baz\"}
{:id :d :label \"bat\"}]")
(defn replace-entry [content id new-content]
,,,)
(replace-entry content :c {:id :c :label "bal" :organization "me"})
=> "[{:id :a :label \"foo\"}
{:id :b :label \"bar\"}
{:id :c :label \"bal\" :organization \"me\"}
{:id :d :label \"bat\"}]"There may be something relevant in https://github.com/borkdude/rewrite-edn
Not sure right away, but perhaps it might give you some inspiration
Thanks, I'll take a look ๐
I think you could combine it at least to move to the map with the right id and then remove or replace it using a zipper operation
In case anyone is interested, this is the closest I came up with...
(defn filterz [zloc f] (z/find zloc #(->> % z/string f)))
(defn replace-entry [content id new-content]
(-> (z/of-string content)
z/down
(filterz #(= (:id (clojure.edn/read-string %)) id))
(z/replace new-content)
z/root-string))
I think it's good enough for now ๐