This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2019-06-01
Channels
- # announcements (7)
- # beginners (30)
- # boot (6)
- # calva (24)
- # cider (8)
- # clj-kondo (8)
- # cljs-dev (16)
- # cljsrn (8)
- # clojure (60)
- # clojure-italy (19)
- # clojure-mexico (6)
- # clojure-nlp (2)
- # clojure-uk (11)
- # clojurescript (36)
- # datomic (11)
- # hoplon (1)
- # juxt (2)
- # kaocha (1)
- # keechma (13)
- # nrepl (1)
- # off-topic (51)
- # reagent (7)
- # rewrite-clj (17)
- # robots (7)
- # shadow-cljs (2)
- # sql (4)
- # testing (1)
- # tools-deps (11)
- # vim (12)
Ok, I have started to have a look. Remember that, surprising at it may be, I’m not yet an expert in rewrite-clj.
So… looking at your first prewalk… I thought you had to transform a zloc with a manipulation function (edit, replace etc)? If I take a simpler example and do something similar, I will get no change:
(def ex2 "[1 2 3 [4 5]]")
(-> (rz/of-string ex2)
(rz/prewalk (fn [zloc]
(println (rz/string zloc))
(= (rz/string zloc) "2"))
(fn [zloc]
(println "-->" (rz/node (rz/right zloc)))
(rz/right zloc)))
(rz/string))
rewrite-clj will not allow you to simply rz/remove
the metadata :map from the :meta node because it does not allow a :meta node without a :map.
Another way that more closely matches your first attempt might be:
(-> (rz/of-string comment-form-with-meta-idea-str)
(rz/prewalk (fn [zloc]
(when (= (rz/tag zloc) :meta)
(let [map-zloc (rz/down zloc)]
(contains? (rz/sexpr map-zloc)
:ael/expected))))
(fn [zloc]
(rz/replace zloc
(-> zloc rz/down rz/right rz/node))))
rz/string)
thanks for the consideration -- i'm even less of an expert, and i just woke up, but i hope to digest your response and get back to you soon 🙂
oh ok… I played a bit more… this makes sense… when we simply return a zloc from prewalk it just changes where we are at in the walk. I’ll modify my simpler example a tiny bit:
(-> (rz/of-string ex2)
(rz/prewalk (fn [zloc]
(println "walking: " (rz/string zloc))
(= (rz/string zloc) "2"))
(fn [zloc]
(rz/right zloc)))
(rz/string))
Which outputs:
walking: [1 2 3 [4 5]]
walking: 1
walking: 2
walking: [4 5]
walking: 4
walking: 5
Notice that our rz/right
has us skipping element 3
in our prewalk traversal.@lee maybe something like this?
(require '[rewrite-clj.zip :as rz])
(def comment-form-with-meta-idea-str
(str "(comment\n"
"\n"
" ^{:ael/expected 0 :ael/name \"simple subtraction\"}\n"
" (- 1 1)\n"
")\n"))
(loop [zloc (rz/of-string comment-form-with-meta-idea-str)]
(if (rz/end? zloc) (rz/root zloc)
(let [t (rz/tag zloc)]
(if (= :meta t)
(recur (-> zloc rz/splice rz/remove))
(recur (rz/next zloc))))))