Skip to content

Instantly share code, notes, and snippets.

@xsc
Created August 7, 2013 10:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xsc/6173036 to your computer and use it in GitHub Desktop.
Save xsc/6173036 to your computer and use it in GitHub Desktop.
node removal utilities for fast-zip
(ns fast-zip-utils
(:require [fast-zip.core :as z])
(:import [fast_zip.core ZipperPath ZipperLocation]))
(defn remove-right
"Remove right sibling of the current node (if there is one)."
[^ZipperLocation zloc]
(let [path ^ZipperPath (.path zloc)]
(if (zero? (count (.r path)))
zloc
(ZipperLocation.
(.branch? zloc)
(.children zloc)
(.make-node zloc)
(.node zloc)
(assoc path :r (clojure.core/next (.r path)) :changed? true)))))
(defn remove-left
"Remove left sibling of the current node (if there is one)."
[^ZipperLocation zloc]
(let [path ^ZipperPath (.path zloc)]
(if (zero? (count (.l path)))
zloc
(ZipperLocation.
(.branch? zloc)
(.children zloc)
(.make-node zloc)
(.node zloc)
(assoc path :l (pop (.l path)) :changed? true)))))
(defn remove-and-move-left
"Remove current node and move left. If current node is at the leftmost
location, no removal is performed and `nil` returned."
[^ZipperLocation zloc]
(let [path ^ZipperPath (.path zloc)]
(when (pos? (count (.l path)))
(ZipperLocation.
(.branch? zloc)
(.children zloc)
(.make-node zloc)
(peek (.l path))
(assoc path :l (pop (.l path)) :changed? true)))))
(defn remove-and-move-right
"Remove current node and move right. If current node is at the rightmost
location, no removal is performed and `nil` returned."
[^ZipperLocation zloc]
(let [path ^ZipperPath (.path zloc)]
(when (pos? (count (.r path)))
(ZipperLocation.
(.branch? zloc)
(.children zloc)
(.make-node zloc)
(first (.r path))
(assoc path :r (clojure.core/next (.r path)) :changed? true)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment