I'm writing a tree ( business process decision tree ) in clojure data structure .
(require clojure.zip :as z)
(z/vector-zip
[ :billed?
[:yes
[:check-bank-account]
[:check-cash] ]
[:send-out-email] ])
when code walk on the first node, it will read the keyword and perform certain actions, the result will be True or False, then I would like it walk into left ( True) or right (False) node .
When my code starts with root node, and call some function associated with :billed? it return a True, how could clojure walk into :yes node, or :send-out-email node ? I thought there is only z/down while, left or rightis only for siblings not for directions of children.
Thank you very much for your time and appreciate any thoughts
Zippers walk through a data structure while keeping track of the current position. To reach different nodes, you have to apply a sequence of moves to the same zipper. When the zipper is created your position is directly above the tree:
So you can descend into the tree with
z/down, and usez/nodeto get the current node from the zipper's location:If you're walking the tree from the top towards some node, you probably only need
z/downandz/right, since descending into a child vector will place you at the leftmost child. It's easier to imagine this if you lay out the vector in a flat line and imaginez/rightsimply moving a cursor to the next element, andz/downmoving the cursor to inside a vector.Here's an example of you could walk this tree by evaluating the keys against a map of facts: