3

The Scalaz Tree class proves seemingly very useful `Zipper' functionality via TreeLoc (Javadoc).

However, it's not apparent to me how to easily iterate through a tree (e.g. to find the `k-th' node in a tree containing a total of n>k nodes) without doing a lot of conditional hedging on whether the zipper is at the end of the list of current children.

Is there an easy way to do this that I'm missing?

NietzscheanAI
  • 966
  • 6
  • 16

1 Answers1

4

Stealing from the TreeLoc.find method, you could do something like this:

  def findAt[A](tree: TreeLoc[A], k: Int): Option[TreeLoc[A]] = {
    Cobind[TreeLoc].cojoin(tree).tree.flatten.drop(k).headOption
  }
Noah
  • 13,821
  • 4
  • 36
  • 45