I’m replacing some vectors with maps. I’m invoking (z/replace zloc m) , which puts comma separators between the entries in m . Is there a way to avoid the commas? (Personally, I’m fine with them, but coworkers seem to look askance at the commas). I guess I need another pass to remove them?
maybe @lee has some ideas on this, but here's how I "uncomma"'d some EDN in jet: https://github.com/borkdude/jet/blob/1b126c051fb858708f30eca6d57eaded172d79b2/src/jet/formats.clj#L66-L74
That seems like a decent approach to me @borkdude! @joel380, in case you didn't know, the commas are coming from Clojure itself:
{:a 1 :b 2 :c 3}
;; => {:a 1, :b 2, :c 3}
A repro of your situation:
(require '[rewrite-clj.zip :as z])
(-> "some code"
z/of-string
z/right
(z/replace {:a 1 :b 2 :c 3})
z/root-string)
;; => "some {:a 1, :b 2, :c 3}"
I don't know where your m map is coming from, so this may not work for you, but you could express your map as rewrite-clj nodes:
(require
'[rewrite-clj.node :as n]
'[rewrite-clj.zip :as z])
(-> "some code"
z/of-string
z/right
(z/replace (n/map-node [(n/keyword-node :a)
(n/spaces 1)
(n/token-node 1)
(n/spaces 1)
(n/keyword-node :b)
(n/spaces 1)
(n/token-node 2)
(n/spaces 1)
(n/keyword-node :c)
(n/spaces 1)
(n/token-node 3)]))
z/root-string)
;; => "some {:a 1 :b 2 :c 3}"
Or express your map as nodes from a parsed string:
(require
'[rewrite-clj.parser :as p]
'[rewrite-clj.zip :as z])
(-> "some code"
z/of-string
z/right
(z/replace (p/parse-string "{:a 1 :b 2 :c 3}"))
z/root-string)
;; => "some {:a 1 :b 2 :c 3}"I like the borkdude’s solution above as then I can theoretically call it on an arbitrary expression. Except I see that depends on z/end? which I think would be the end of the string/file? Is there a way to change that so that it’s applied to the “current expression” only?
I'm not sure if there's another predicate to check the current expression's end, have you scanned the rest of the API?
I see a z/of-node not sure what that does though.
maybe this is what subzip is for
https://cljdoc.org/d/rewrite-clj/rewrite-clj/1.1.47/doc/user-guide#sub-editing
That looks promising. So I z/replace my map in, and still should be able to use the same zloc to subzip the commas out with pretty much your code.
Yep, that's the intent of sub editing.
For interested lurkers, a rewrite-clj discussion in #grasp https://clojurians.slack.com/archives/C01EF12T49W/p1719242956880299