File tree Expand file tree Collapse file tree 2 files changed +37
-4
lines changed Expand file tree Collapse file tree 2 files changed +37
-4
lines changed Original file line number Diff line number Diff line change 64
64
(defn cljx-walk
65
65
" Replace all occurences of profile reader macros."
66
66
[root active-profiles]
67
- (loop [loc root]
68
- (if-let [mloc (z/find loc z/next cljx-macro?)]
69
- (recur (handle-reader-macro active-profiles mloc))
70
- loc)))
67
+ (z/prewalk root cljx-macro? #(handle-reader-macro active-profiles %)))
71
68
72
69
; ; ## Test
73
70
Original file line number Diff line number Diff line change 379
379
(if (map? zloc)
380
380
(-> zloc (append-child k) (append-child v))
381
381
(throw (Exception. (str " not a valid index to assoc: " v))))))
382
+
383
+ ; ; ## Edit Scope
384
+
385
+ (defn subzip
386
+ " Create zipper that only contains the given zipper location's node."
387
+ [zloc]
388
+ (when zloc
389
+ (edn (z/node zloc))))
390
+
391
+ (defmacro edit->
392
+ " Will pass arguments to `->`. Return value will be the state of the input node
393
+ after all modifications have been performed. This means that the result is
394
+ automatically 'zipped up' to represent the same location the macro was given."
395
+ [zloc & body]
396
+ `(let [zloc# ~zloc
397
+ loc# (subzip zloc#)]
398
+ (if-let [edit# (-> loc# ~@body)]
399
+ (z/replace zloc# (z/root edit#))
400
+ (throw (Exception. " Body of edit-> did not return value." )))))
401
+
402
+ ; ; ## Walk
403
+
404
+ (defn prewalk
405
+ " Perform a depth-first pre-order traversal starting at the given zipper location
406
+ and apply the given function to each child node. If a predicate `p?` is given,
407
+ only apply the function to nodes matching it."
408
+ ([zloc f] (prewalk zloc (constantly true ) f))
409
+ ([zloc p? f]
410
+ (loop [loc (subzip zloc)]
411
+ (if (z/end? loc)
412
+ (z/replace zloc (z/root loc))
413
+ (if-let [n0 (find loc next p?)]
414
+ (if-let [n1 (f n0)]
415
+ (recur (z/next n1))
416
+ (recur (z/next n0)))
417
+ (z/replace zloc (z/root loc)))))))
You can’t perform that action at this time.
0 commit comments