rewrite-clj

https://github.com/clj-commons/rewrite-clj
lread 2019-06-01T19:31:00.021300Z

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:

lread 2019-06-01T19:36:11.024700Z

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

lread 2019-06-01T19:47:14.026500Z

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.

lread 2019-06-01T19:47:39.026900Z

So maybe your 2nd version is not too shabby?

lread 2019-06-01T19:49:18.028100Z

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)

lread 2019-06-01T19:50:35.028500Z

Looking forward to reading what you think.

2019-06-01T19:51:27.029500Z

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 🙂

lread 2019-06-01T19:52:06.030200Z

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

2019-06-01T19:53:12.030900Z

quite nice 🙂

lread 2019-06-01T20:19:54.034Z

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.

lread 2019-06-01T21:20:49.035100Z

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

borkdude 2019-06-01T21:57:48.035500Z

@lee are you trying to get rid of metadata?

borkdude 2019-06-01T22:14:35.036Z

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

borkdude 2019-06-01T22:15:18.036200Z

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

lread 2019-06-01T22:16:07.037100Z

nah, not me, @sogaiu , just helping out

borkdude 2019-06-01T22:17:24.037500Z

sorry misread. but it seems to work @sogaiu 🙂

lread 2019-06-01T22:21:05.038700Z

coolio, thanks!