Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find location of node in tree using Clojure zippers

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.

like image 848
KendallB Avatar asked Feb 01 '14 00:02

KendallB


1 Answers

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.
like image 158
Arthur Ulfeldt Avatar answered Nov 07 '22 19:11

Arthur Ulfeldt