Skip to content

Instantly share code, notes, and snippets.

@hughfdjackson
Created November 3, 2012 17:25
Show Gist options
  • Select an option

  • Save hughfdjackson/4008021 to your computer and use it in GitHub Desktop.

Select an option

Save hughfdjackson/4008021 to your computer and use it in GitHub Desktop.
(require '[clojure.zip :as z])
; make a tree using vectors
(def data [1 [2 3]]) ;
data ; => [1 [2 3]]
; make a zipped version
(def dz (z/vector-zip data))
dz ; => [[1 [2 3]] nil] (nil for where the cursor data will be; but we haven't traversed anywhere)
; traverse it! (the -> macro is like an inverse compose)
(-> dz z/down z/right z/down) ; => [2 {:l [], :pnodes [[1 [2 3]] [2 3]], :ppath {:l [1], :pnodes [[1 [2 3]]], :ppath nil, :r nil}, :r (3)}]
; get the node back out from the point of the cursor
(-> dz z/down z/right z/down z/node) ; => 2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment