diff options
author | Rich Hickey <richhickey@gmail.com> | 2009-02-09 18:17:05 +0000 |
---|---|---|
committer | Rich Hickey <richhickey@gmail.com> | 2009-02-09 18:17:05 +0000 |
commit | 80c1197b2a21722e6c3e36884e1207de224f063d (patch) | |
tree | 7a11603a5f527211dcad875b9e7aeb33ca6cac27 /src | |
parent | c68fe476441fed71c2788a284f1c3081813502f6 (diff) |
zip/remove does not return the correct loc, patch from cgrand
fixed refer-clojure doc
Diffstat (limited to 'src')
-rw-r--r-- | src/clj/clojure/core.clj | 2 | ||||
-rw-r--r-- | src/clj/clojure/zip.clj | 34 |
2 files changed, 33 insertions, 3 deletions
diff --git a/src/clj/clojure/core.clj b/src/clj/clojure/core.clj index b958b7eb..fc31be7c 100644 --- a/src/clj/clojure/core.clj +++ b/src/clj/clojure/core.clj @@ -3388,7 +3388,7 @@ ~@(map process-reference references)))) (defmacro refer-clojure - "Same as (refer 'clojure <filters>)" + "Same as (refer 'clojure.core <filters>)" [& filters] `(clojure.core/refer '~'clojure.core ~@filters)) diff --git a/src/clj/clojure/zip.clj b/src/clj/clojure/zip.clj index 1333ae1e..65e57559 100644 --- a/src/clj/clojure/zip.clj +++ b/src/clj/clojure/zip.clj @@ -127,6 +127,14 @@ (when (and path rs) (with-meta [r (assoc path :l (conj l node) :r rrest)] ^loc)))) +(defn rightmost + "Returns the loc of the rightmost sibling of the node at this loc, or self" + [loc] + (let [[node {l :l r :r :as path}] loc] + (if (and path r) + (with-meta [(last r) (assoc path :l (apply conj l node (butlast r)) :r nil)] ^loc) + loc))) + (defn left "Returns the loc of the left sibling of the node at this loc, or nil" [loc] @@ -134,6 +142,14 @@ (when (and path (seq l)) (with-meta [(peek l) (assoc path :l (pop l) :r (cons node r))] ^loc)))) +(defn leftmost + "Returns the loc of the leftmost sibling of the node at this loc, or self" + [loc] + (let [[node {l :l r :r :as path}] loc] + (if (and path (seq l)) + (with-meta [(first l) (assoc path :l [] :r (concat (rest l) [node] r))] ^loc) + loc))) + (defn insert-left "Inserts the item as the left sibling of the node at this loc, without moving" @@ -190,6 +206,17 @@ (or (right (up p)) (recur (up p))) [(node p) :end]))))) +(defn prev + "Moves to the previous loc in the hierarchy, depth-first. If already + at the root, returns nil." + [loc] + (if-let [lloc (left loc)] + (loop [loc lloc] + (if (branch? loc) + (recur (-> loc down rightmost)) + loc)) + (up loc))) + (defn end? "Returns true if loc represents the end of a depth-first walk" [loc] @@ -202,8 +229,11 @@ (let [[node {l :l, ppath :ppath, pnodes :pnodes, rs :r, :as path}] loc] (if (nil? path) (throw (new Exception "Remove at top")) - (if (pos? (count l)) - (with-meta [(peek l) (assoc path :l (pop l) :changed? true)] ^loc) + (if (pos? (count l)) + (loop [loc (with-meta [(peek l) (assoc path :l (pop l) :changed? true)] ^loc)] + (if (branch? loc) + (recur (-> loc down rightmost)) + loc)) (with-meta [(make-node loc (peek pnodes) rs) (and ppath (assoc ppath :changed? true))] ^loc))))) |