Find location of node in tree using Clojure zippers

clojure, tree, zipper

Solution

Yes! This is exactly the kind of task zippers where designed for.

- Repeatedly call zip/next until you find the node you are looking for.

- Then call `zip/path` to find out where you are relative to the root.

- Then call `zip/up`, `zip/down`, `zip/left` etc to get to the node to modify.

- update the node

- call `zip/root` to get your new map containing these changes.

Problem

I have a tree of an unknown structure. First, I want to find a node containing a string of text, "Something". Then, after identifying the string's location in the tree, I want to update a different node relative to the string's location. The data is a deeply nested map with several branches of lists. Is that possible with zippers? I've studied this approach to editing trees: http://www.exampler.com/blog/2010/09/01/editing-trees-in-clojure-with-clojurezip/. Problem is, I don't beforehand know the location of the string.

Original source