Debian Codegraph @

src/cljs/fast_zip/core.cljs

raw
1 ; Copyright (c) Rich Hickey. All rights reserved.
2 ; The use and distribution terms for this software are covered by the
3 ; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
4 ; which can be found in the file epl-v10.html at the root of this distribution.
5 ; By using this software in any fashion, you are agreeing to be bound by
6 ; the terms of this license.
7 ; You must not remove this notice, or any other, from this software.
8
9 ;functional hierarchical zipper, with navigation, editing and enumeration
10 ;see Huet
11
12 (ns ^{:doc "Functional hierarchical zipper, with navigation, editing,
13 and enumeration. See Huet"
14 :author "Rich Hickey, modified by Alexander K. Hudek"}
15 fast-zip.core
16 (:refer-clojure :exclude [replace remove next]))
17
18 (defrecord ZipperPath [l r ppath pnodes changed?])
19
20 (defrecord ZipperLocation [branch? children make-node node path])
21
22 (defn zipper
23 "Creates a new zipper structure.
24
25 branch? is a fn that, given a node, returns true if can have
26 children, even if it currently doesn't.
27
28 children is a fn that, given a branch node, returns a seq of its
29 children.
30
31 make-node is a fn that, given an existing node and a seq of
32 children, returns a new branch node with the supplied children.
33 root is the root node."
34 {:added "1.0"}
35 [branch? children make-node root]
36 (ZipperLocation. branch? children make-node root nil))
37
38 (defn seq-zip
39 "Returns a zipper for nested sequences, given a root sequence"
40 {:added "1.0"}
41 [root]
42 (zipper seq?
43 identity
44 (fn [node children] (with-meta children (meta node)))
45 root))
46
47 (defn vector-zip
48 "Returns a zipper for nested vectors, given a root vector"
49 {:added "1.0"}
50 [root]
51 (zipper
52 vector?
53 seq
54 (fn [node children] (with-meta (vec children) (meta node)))
55 root))
56
57 (defn xml-zip
58 "Returns a zipper for xml elements (as from xml/parse),
59 given a root element"
60 {:added "1.0"}
61 [root]
62 (zipper (complement string?)
63 (comp seq :content)
64 (fn [node children]
65 (assoc node :content (and children (apply vector children))))
66 root))
67
68 (defn node
69 "Returns the node at loc"
70 [^ZipperLocation loc]
71 (.-node loc))
72
73 (defn branch?
74 "Returns true if the node at loc is a branch"
75 [^ZipperLocation loc]
76 ((.-branch? loc) (.-node loc)))
77
78 (defn children
79 "Returns a seq of the children of node at loc, which must be a branch"
80 [^ZipperLocation loc]
81 ((.-children loc) (.-node loc)))
82
83 (defn make-node
84 "Returns a new branch node, given an existing node and new children.
85 The loc is only used to supply the constructor."
86 [^ZipperLocation loc node children]
87 ((.-make-node loc) node children))
88
89 (defn path
90 "Returns a seq of nodes leading to this loc"
91 [^ZipperLocation loc]
92 (if-let [p (.-path loc)] (.-pnodes ^ZipperPath p)))
93
94 (defn lefts
95 "Returns a seq of the left siblings of this loc"
96 [^ZipperLocation loc]
97 (if-let [p (.-path loc)] (seq (reverse (.-l ^ZipperPath p)))))
98
99 (defn rights
100 "Returns a seq of the right siblings of this loc"
101 [^ZipperLocation loc]
102 (if-let [p (.-path loc)] (.-r ^ZipperPath p)))
103
104 (defn down
105 "Returns the loc of the leftmost child of the node at this loc,
106 or nil if no children"
107 [^ZipperLocation loc]
108 (when (branch? loc)
109 (when-let [cs (children loc)]
110 (let [node (.-node loc), path ^ZipperPath (.-path loc)]
111 (ZipperLocation.
112 (.-branch? loc)
113 (.-children loc)
114 (.-make-node loc)
115 (first cs)
116 (ZipperPath. '() (clojure.core/next cs) path (if path (conj (.-pnodes path) node) [node]) nil))))))
117
118 (defn up
119 "Returns the loc of the parent of the node at this loc, or nil if at the top"
120 [^ZipperLocation loc]
121 (let [node (.-node loc), path ^ZipperPath (.-path loc)]
122 (when-let [pnodes (and path (.-pnodes path))]
123 (let [pnode (peek pnodes)]
124 (if (.-changed? path)
125 (ZipperLocation.
126 (.-branch? loc)
127 (.-children loc)
128 (.-make-node loc)
129 (make-node loc pnode (concat (reverse (.-l path)) (cons node (.-r path))))
130 (if-let [ppath (.-ppath path)] (assoc ppath :changed? true)))
131 (ZipperLocation.
132 (.-branch? loc)
133 (.-children loc)
134 (.-make-node loc)
135 pnode
136 (.-ppath path)))))))
137
138 (defn root
139 "zips all the way up and returns the root node, reflecting any changes."
140 [^ZipperLocation loc]
141 (if (= :end (.-path loc))
142 (.-node loc)
143 (let [p (up loc)]
144 (if p
145 (recur p)
146 (.-node loc)))))
147
148 (defn right
149 "Returns the loc of the right sibling of the node at this loc, or nil"
150 [^ZipperLocation loc]
151 (let [path ^ZipperPath (.-path loc)]
152 (when-let [r (and path (.-r path))]
153 (ZipperLocation.
154 (.-branch? loc)
155 (.-children loc)
156 (.-make-node loc)
157 (first r)
158 (assoc path :l (conj (.-l path) (.-node loc)) :r (clojure.core/next r))))))
159
160 (defn rightmost
161 "Returns the loc of the rightmost sibling of the node at this loc, or self"
162 [^ZipperLocation loc]
163 (let [path ^ZipperPath (.-path loc)]
164 (if-let [r (and path (.-r path))]
165 (ZipperLocation.
166 (.-branch? loc)
167 (.-children loc)
168 (.-make-node loc)
169 (last r)
170 (assoc path :l (apply conj (.-l path) (.-node loc) (butlast r)) :r nil))
171 loc)))
172
173 (defn left
174 "Returns the loc of the left sibling of the node at this loc, or nil"
175 [^ZipperLocation loc]
176 (let [path ^ZipperPath (.-path loc)]
177 (when (and path (seq (.-l path)))
178 (ZipperLocation.
179 (.-branch? loc)
180 (.-children loc)
181 (.-make-node loc)
182 (peek (.-l path))
183 (assoc path :l (pop (.-l path)) :r (cons (.-node loc) (.-r path)))))))
184
185 (defn leftmost
186 "Returns the loc of the leftmost sibling of the node at this loc, or self"
187 [^ZipperLocation loc]
188 (let [path ^ZipperPath (.-path loc)]
189 (if (and path (seq (.-l path)))
190 (ZipperLocation.
191 (.-branch? loc)
192 (.-children loc)
193 (.-make-node loc)
194 (peek (.-l path))
195 (assoc path :l [] :r (concat (clojure.core/next (reverse (.-l path))) [(.-node loc)] (.-r path))))
196 loc)))
197
198 (defn insert-left
199 "Inserts the item as the left sibling of the node at this loc, without moving"
200 [^ZipperLocation loc item]
201 (if-let [path ^ZipperPath (.-path loc)]
202 (ZipperLocation.
203 (.-branch? loc)
204 (.-children loc)
205 (.-make-node loc)
206 (.-node loc)
207 (assoc path :l (conj (.-l path) item) :changed? true))
208 (throw (new js/Error "Insert at top"))))
209
210 (defn insert-right
211 "Inserts the item as the right sibling of the node at this loc, without moving"
212 [^ZipperLocation loc item]
213 (if-let [path ^ZipperPath (.-path loc)]
214 (ZipperLocation.
215 (.-branch? loc)
216 (.-children loc)
217 (.-make-node loc)
218 (.-node loc)
219 (assoc path :r (cons item (.-r path)) :changed? true))
220 (throw (new js/Error "Insert at top"))))
221
222 (defn replace
223 "Replaces the node at this loc, without moving"
224 [^ZipperLocation loc node]
225 (ZipperLocation.
226 (.-branch? loc)
227 (.-children loc)
228 (.-make-node loc)
229 node
230 (if-let [path (.-path loc)] (assoc path :changed? true))))
231
232 (defn insert-child
233 "Inserts the item as the leftmost child of the node at this loc, without moving"
234 [^ZipperLocation loc item]
235 (replace loc (make-node loc (.-node loc) (cons item (children loc)))))
236
237 (defn append-child
238 "Inserts the item as the rightmost child of the node at this loc, without moving"
239 [^ZipperLocation loc item]
240 (replace loc (make-node loc (.-node loc) (concat (children loc) [item]))))
241
242 (defn next
243 "Moves to the next loc in the hierarchy, depth-first. When reaching
244 the end, returns a distinguished loc detectable via end?. If already
245 at the end, stays there."
246 [^ZipperLocation loc]
247 (let [path (.-path loc)]
248 (if (= :end path)
249 loc
250 (or
251 (and (branch? loc) (down loc))
252 (right loc)
253 (loop [p loc]
254 (if (up p)
255 (or (right (up p)) (recur (up p)))
256 (ZipperLocation. (.-branch? loc) (.-children loc) (.-make-node loc) (.-node p) :end)))))))
257
258 (defn prev
259 "Moves to the previous loc in the hierarchy, depth-first. If already at the root, returns nil."
260 [loc]
261 (if-let [lloc (left loc)]
262 (loop [loc lloc]
263 (if-let [child (and (branch? loc) (down loc))]
264 (recur (rightmost child))
265 loc))
266 (up loc)))
267
268 (defn end?
269 "Returns true if loc represents the end of a depth-first walk"
270 [^ZipperLocation loc]
271 (= :end (.-path loc)))
272
273 (defn remove
274 "Removes the node at loc, returning the loc that would have preceded it in a depth-first walk."
275 [^ZipperLocation loc]
276 (if-let [path ^ZipperPath (.-path loc)]
277 (if (pos? (count (.-l path)))
278 (loop [loc (ZipperLocation.
279 (.-branch? loc)
280 (.-children loc)
281 (.-make-node loc)
282 (peek (.-l path))
283 (assoc path :l (pop (.-l path)) :changed? true))]
284 (if-let [child (and (branch? loc) (down loc))]
285 (recur (rightmost child))
286 loc))
287 (ZipperLocation.
288 (.-branch? loc)
289 (.-children loc)
290 (.-make-node loc)
291 (make-node loc (peek (.-pnodes path)) (.-r path))
292 (if-let [ppath (.-ppath path)] (and ppath (assoc ppath :changed? true)))))
293 (throw (new js/Error "Remove at top"))))
294
295 (defn edit
296 "Replaces the node at this loc with the value of (f node args)"
297 [^ZipperLocation loc f & args]
298 (replace loc (apply f (.-node loc) args)))