File tree Expand file tree Collapse file tree 3 files changed +16
-21
lines changed Expand file tree Collapse file tree 3 files changed +16
-21
lines changed Original file line number Diff line number Diff line change @@ -131,22 +131,13 @@ Search functions include:
131
131
location (default movement: ` rewrite-clj.zip/right ` ). This might return ` zloc ` itself.
132
132
- ` (find-next zloc [f] p?) ` : find the next match for the given predicate by repeatedly applying ` f ` to the current zipper
133
133
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.
134
136
- ` (find-token zloc [f] p?): like ` find` but will only check ` : token ` nodes. The predicate is applied to the node's value.
135
137
- ` (find-next-token zloc [f] p?): like ` find-next` but will only check ` : token ` nodes.
136
138
- ` (find-value zloc [f] v) ` : uses ` find ` to get the first ` :token ` node with the given value.
137
139
- ` (find-next-value zloc [f] v) ` : uses ` find-next ` to get the first ` :token ` node with the given value.
138
140
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
-
150
141
### Handling Clojure Data Structures
151
142
152
143
rewrite-clj aims at providing easy ways to work with Clojure data structures. It offers functions corresponding
Original file line number Diff line number Diff line change 192
192
(when-let [zloc (f zloc)]
193
193
(find zloc f p?))))
194
194
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))))
202
206
203
207
(defn find-token
204
208
" Find token element matching the given predicate by applying the given movement function
Original file line number Diff line number Diff line change 86
86
(-> root (z/find-value z/next :description ) z/right z/node) => [:token " A project." ]
87
87
(-> root (z/find-value z/next " private" ) z/right z/node) => [:token " http://private.com/repo" ]
88
88
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))
91
91
=> ['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))
93
93
=> [:repositories :dependencies " A project." :description " 0.1.0-SNAPSHOT" 'my-project 'defproject])
94
94
95
95
(fact " about zipper seq operations"
You can’t perform that action at this time.
0 commit comments