Fork me on GitHub
#rewrite-clj
<
2019-06-01
>
lread19:06:00

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

lread19:06:11

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))

lread19:06:14

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.

lread19:06:39

So maybe your 2nd version is not too shabby?

lread19:06:18

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)

lread19:06:35

Looking forward to reading what you think.

sogaiu19:06:27

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 🙂

lread19:06:06

Sure, take your time. We can continue to learn together!

sogaiu19:06:12

quite nice 🙂

lread20:06:54

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.

lread21:06:49

Which does not affect the resulting string: "[1 2 3 [4 5]]".

borkdude21:06:48

@lee are you trying to get rid of metadata?

borkdude22:06:35

@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))))))

borkdude22:06:18

returns: "(comment\n\n (- 1 1)\n)\n"

lread22:06:07

nah, not me, @sogaiu , just helping out

borkdude22:06:24

sorry misread. but it seems to work @sogaiu 🙂

lread22:06:05

coolio, thanks!