This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2022-11-04
Channels
- # announcements (30)
- # aws (7)
- # babashka (7)
- # beginners (64)
- # calva (39)
- # cherry (17)
- # cider (1)
- # clj-on-windows (6)
- # clojure (30)
- # clojure-austin (12)
- # clojure-europe (25)
- # clojure-nl (2)
- # clojure-norway (23)
- # clojure-spec (23)
- # clojure-uk (6)
- # clojurescript (20)
- # cursive (18)
- # datahike (3)
- # datalevin (12)
- # datomic (9)
- # etaoin (5)
- # graalvm (45)
- # instaparse (2)
- # interceptors (11)
- # kaocha (1)
- # lsp (102)
- # meander (6)
- # nbb (16)
- # off-topic (30)
- # pathom (83)
- # pedestal (6)
- # portal (5)
- # re-frame (12)
- # reitit (5)
- # rewrite-clj (10)
- # scittle (35)
- # shadow-cljs (49)
- # spacemacs (10)
- # vim (14)
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 ๐งต
(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
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 ๐I'll look it up, thanks