Ok, I have started to have a look. Remember that, surprising at it may be, I’m not yet an expert in rewrite-clj. :simple_smile:
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.
So maybe your 2nd version is not too shabby?
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)
Looking forward to reading what you think.
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 🙂
Sure, take your time. We can continue to learn together!
quite nice 🙂
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.Which does not affect the resulting string: "[1 2 3 [4 5]]"
.
@lee are you trying to get rid of metadata?
@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))))))
returns: "(comment\n\n (- 1 1)\n)\n"
nah, not me, @sogaiu , just helping out
sorry misread. but it seems to work @sogaiu 🙂
coolio, thanks!