Skip to content

Commit 6e5e126

Browse files
author
Yannick Scherer
committed
Removed tag=/value= and added find-tag/find-next-tag.
1 parent 2add2a6 commit 6e5e126

File tree

3 files changed

+16
-21
lines changed

3 files changed

+16
-21
lines changed

README.md

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -131,22 +131,13 @@ Search functions include:
131131
location (default movement: `rewrite-clj.zip/right`). This might return `zloc` itself.
132132
- `(find-next zloc [f] p?)`: find the next match for the given predicate by repeatedly applying `f` to the current zipper
133133
location (default movement: `rewrite-clj.zip/right`). This will not return `zloc` itself.
134+
- `(find-tag zloc [f] t)`: uses `find` to get the first node with the given tag.
135+
- `(find-next-tag zloc [f] t)`: uses `find-next` to get the first node with the given tag.
134136
- `(find-token zloc [f] p?): like `find` but will only check `:token` nodes. The predicate is applied to the node's value.
135137
- `(find-next-token zloc [f] p?): like `find-next` but will only check `:token` nodes.
136138
- `(find-value zloc [f] v)`: uses `find` to get the first `:token` node with the given value.
137139
- `(find-next-value zloc [f] v)`: uses `find-next` to get the first `:token` node with the given value.
138140

139-
The following two functions are designed to be used as predicates in `find` and `find-next`:
140-
141-
- `(tag= t)`: creates a predicate that checks a zipper node against the given tag.
142-
- `(value= t)`: creates a predicate that checks a zipper node's value against the given one.
143-
144-
Example:
145-
146-
```clojure
147-
(-> zloc z/down (z/find (z/tag= :list)) z/tag) ;; => :list
148-
```
149-
150141
### Handling Clojure Data Structures
151142

152143
rewrite-clj aims at providing easy ways to work with Clojure data structures. It offers functions corresponding

src/rewrite_clj/zip.clj

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -192,13 +192,17 @@
192192
(when-let [zloc (f zloc)]
193193
(find zloc f p?))))
194194

195-
(defn tag= [t] #(= (tag %) t))
196-
(defn value= [v] #(and (= (tag %) :token) (= (value %) v)))
197-
198-
(comment
199-
;; Usage:
200-
(-> zloc down (find (tag= :list)))
201-
(-> zloc (find-next (value= :description))))
195+
(defn find-tag
196+
"Find element with the given tag by applying the given movement function to the initial
197+
zipper location."
198+
([zloc t] (find-tag zloc right t))
199+
([zloc f t] (find zloc f #(= (tag %) t))))
200+
201+
(defn find-next-tag
202+
"Find element other than the current zipper location with the given tag by applying the
203+
given movement function to the initial zipper location."
204+
([zloc t] (find-next-tag zloc right t))
205+
([zloc f t] (find-next zloc f #(= (tag %) t))))
202206

203207
(defn find-token
204208
"Find token element matching the given predicate by applying the given movement function

test/rewrite_clj/zip_test.clj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,10 @@
8686
(-> root (z/find-value z/next :description) z/right z/node) => [:token "A project."]
8787
(-> root (z/find-value z/next "private") z/right z/node) => [:token "http://private.com/repo"]
8888

89-
(-> root (z/find z/next (z/tag= :map)) z/down z/node) => [:token "private"]
90-
(->> root z/down (iterate #(z/find-next % (z/tag= :token))) (take-while identity) (map z/node) (map second))
89+
(-> root (z/find-tag z/next :map) z/down z/node) => [:token "private"]
90+
(->> root z/down (iterate #(z/find-next-tag % :token)) (take-while identity) (map z/node) (map second))
9191
=> ['defproject 'my-project "0.1.0-SNAPSHOT" :description "A project." :dependencies :repositories]
92-
(->> root z/down z/rightmost (iterate #(z/find-next % z/left (z/tag= :token))) (rest) (take-while identity) (map z/node) (map second))
92+
(->> root z/down z/rightmost (iterate #(z/find-next-tag % z/left :token)) (rest) (take-while identity) (map z/node) (map second))
9393
=> [:repositories :dependencies "A project." :description "0.1.0-SNAPSHOT" 'my-project 'defproject])
9494

9595
(fact "about zipper seq operations"

0 commit comments

Comments
 (0)