|
61 | 61 | (defn branch? |
62 | 62 | "Returns true if the node at loc is a branch" |
63 | 63 | [loc] |
64 | | - ((:zip/branch? ^loc) (node loc))) |
| 64 | + ((:zip/branch? (meta loc)) (node loc))) |
65 | 65 |
|
66 | 66 | (defn children |
67 | 67 | "Returns a seq of the children of node at loc, which must be a branch" |
68 | 68 | [loc] |
69 | 69 | (if (branch? loc) |
70 | | - ((:zip/children ^loc) (node loc)) |
| 70 | + ((:zip/children (meta loc)) (node loc)) |
71 | 71 | (throw (Exception. "called children on a leaf node")))) |
72 | 72 |
|
73 | 73 | (defn make-node |
74 | 74 | "Returns a new branch node, given an existing node and new |
75 | 75 | children. The loc is only used to supply the constructor." |
76 | 76 | [loc node children] |
77 | | - ((:zip/make-node ^loc) node children)) |
| 77 | + ((:zip/make-node (meta loc)) node children)) |
78 | 78 |
|
79 | 79 | (defn path |
80 | 80 | "Returns a seq of nodes leading to this loc" |
|
103 | 103 | (with-meta [c {:l [] |
104 | 104 | :pnodes (if path (conj (:pnodes path) node) [node]) |
105 | 105 | :ppath path |
106 | | - :r cnext}] ^loc))))) |
| 106 | + :r cnext}] (meta loc)))))) |
107 | 107 |
|
108 | 108 | (defn up |
109 | 109 | "Returns the loc of the parent of the node at this loc, or nil if at |
|
116 | 116 | [(make-node loc pnode (concat l (cons node r))) |
117 | 117 | (and ppath (assoc ppath :changed? true))] |
118 | 118 | [pnode ppath]) |
119 | | - ^loc))))) |
| 119 | + (meta loc)))))) |
120 | 120 |
|
121 | 121 | (defn root |
122 | 122 | "zips all the way up and returns the root node, reflecting any |
|
134 | 134 | [loc] |
135 | 135 | (let [[node {l :l [r & rnext :as rs] :r :as path}] loc] |
136 | 136 | (when (and path rs) |
137 | | - (with-meta [r (assoc path :l (conj l node) :r rnext)] ^loc)))) |
| 137 | + (with-meta [r (assoc path :l (conj l node) :r rnext)] (meta loc))))) |
138 | 138 |
|
139 | 139 | (defn rightmost |
140 | 140 | "Returns the loc of the rightmost sibling of the node at this loc, or self" |
141 | 141 | [loc] |
142 | 142 | (let [[node {l :l r :r :as path}] loc] |
143 | 143 | (if (and path r) |
144 | | - (with-meta [(last r) (assoc path :l (apply conj l node (butlast r)) :r nil)] ^loc) |
| 144 | + (with-meta [(last r) (assoc path :l (apply conj l node (butlast r)) :r nil)] (meta loc)) |
145 | 145 | loc))) |
146 | 146 |
|
147 | 147 | (defn left |
148 | 148 | "Returns the loc of the left sibling of the node at this loc, or nil" |
149 | 149 | [loc] |
150 | 150 | (let [[node {l :l r :r :as path}] loc] |
151 | 151 | (when (and path (seq l)) |
152 | | - (with-meta [(peek l) (assoc path :l (pop l) :r (cons node r))] ^loc)))) |
| 152 | + (with-meta [(peek l) (assoc path :l (pop l) :r (cons node r))] (meta loc))))) |
153 | 153 |
|
154 | 154 | (defn leftmost |
155 | 155 | "Returns the loc of the leftmost sibling of the node at this loc, or self" |
156 | 156 | [loc] |
157 | 157 | (let [[node {l :l r :r :as path}] loc] |
158 | 158 | (if (and path (seq l)) |
159 | | - (with-meta [(first l) (assoc path :l [] :r (concat (rest l) [node] r))] ^loc) |
| 159 | + (with-meta [(first l) (assoc path :l [] :r (concat (rest l) [node] r))] (meta loc)) |
160 | 160 | loc))) |
161 | 161 |
|
162 | 162 | (defn insert-left |
|
166 | 166 | (let [[node {l :l :as path}] loc] |
167 | 167 | (if (nil? path) |
168 | 168 | (throw (new Exception "Insert at top")) |
169 | | - (with-meta [node (assoc path :l (conj l item) :changed? true)] ^loc)))) |
| 169 | + (with-meta [node (assoc path :l (conj l item) :changed? true)] (meta loc))))) |
170 | 170 |
|
171 | 171 | (defn insert-right |
172 | 172 | "Inserts the item as the right sibling of the node at this loc, |
|
175 | 175 | (let [[node {r :r :as path}] loc] |
176 | 176 | (if (nil? path) |
177 | 177 | (throw (new Exception "Insert at top")) |
178 | | - (with-meta [node (assoc path :r (cons item r) :changed? true)] ^loc)))) |
| 178 | + (with-meta [node (assoc path :r (cons item r) :changed? true)] (meta loc))))) |
179 | 179 |
|
180 | 180 | (defn replace |
181 | 181 | "Replaces the node at this loc, without moving" |
182 | 182 | [loc node] |
183 | 183 | (let [[_ path] loc] |
184 | | - (with-meta [node (assoc path :changed? true)] ^loc))) |
| 184 | + (with-meta [node (assoc path :changed? true)] (meta loc)))) |
185 | 185 |
|
186 | 186 | (defn edit |
187 | 187 | "Replaces the node at this loc with the value of (f node args)" |
|
239 | 239 | (if (nil? path) |
240 | 240 | (throw (new Exception "Remove at top")) |
241 | 241 | (if (pos? (count l)) |
242 | | - (loop [loc (with-meta [(peek l) (assoc path :l (pop l) :changed? true)] ^loc)] |
| 242 | + (loop [loc (with-meta [(peek l) (assoc path :l (pop l) :changed? true)] (meta loc))] |
243 | 243 | (if-let [child (and (branch? loc) (down loc))] |
244 | 244 | (recur (rightmost child)) |
245 | 245 | loc)) |
246 | 246 | (with-meta [(make-node loc (peek pnodes) rs) |
247 | 247 | (and ppath (assoc ppath :changed? true))] |
248 | | - ^loc))))) |
| 248 | + (meta loc)))))) |
249 | 249 |
|
250 | 250 | (comment |
251 | 251 |
|
|
0 commit comments