我有一个路径列表:
val paths = List(List("foo"), List("bar", "a"), List("bar", "b"))
我想在 scalaz 树中表示它:
def pathsToTree(root: String, paths: List[List[String]]): Tree[String] = ???
结果:
pathsToTree("paths", paths)
=> "paths".node("foo".leaf, "bar".node("a".leaf, "b".leaf))
我TreeLoc
从http://eed3si9n.com/learning-scalaz/Tree.html读到了一些内容,但使用左/右或子索引似乎很乏味。我想像做这样的事情:
paths.foldLeft(root.node()) { case (acc: Tree[String], path: List[String]) =>
acc // how to add all the items from `path` to the tree?
}
看起来我可以使用find
and setTree
ormodifyTree
但这似乎效率很低。