Skip to content

Instantly share code, notes, and snippets.

@devth
Last active August 29, 2015 14:20
Show Gist options
  • Save devth/a82fdfd7b17bc852c372 to your computer and use it in GitHub Desktop.
Save devth/a82fdfd7b17bc852c372 to your computer and use it in GitHub Desktop.
import scalaz._, Scalaz._
val expected = "foo".node("bar".leaf, "baz".node("qux".leaf))
val loc = expected.loc
loc.firstChild
loc.firstChild.right
loc.getChild(2) >>= {_.getChild(1)} >>= {_.getLabel.some}
val mod = loc.getChild(2) >>= {_.getChild(1)} >>= {_.modifyLabel({_ => "hat"}).some}
mod.get.toTree.draw foreach {_.println}
mod.get.toTree.drawTree
mod.find { c => c.getLabel == "hat" }
val paths = List(List("foo"), List("bar", "a"), List("bar", "b"))
expected.map { n =>
println(n)
n.show + "LOL"
}.drawTree
expected.subForest.toList
expected.loc
expected.drawTree
val loc = expected.loc
loc.cojoin.toTree.flatten.filter(_.isLeaf).map(_.path.force).force
loc.toTree.flatten.toList
leafPaths(expected).map(_.force).force
leafPaths(expected).map(_.map(_.getClass).force).force
expected.loc.map { println }
expected.loc.cojoin.toTree.foldMap { loc =>
println(loc)
loc.toString
}
expected.foldMap { n =>
println(n)
n.toString
}
expected.flatten.force
expected.subForest.foreach { subTree =>
visitTree(subTree) { (item: String, path: Stream[String], isLeaf: Boolean) =>
println(s"${(path :+ item).mkString(".")} ${if (isLeaf) "[leaf]" else ""}")
}
}
expected.drawTree
expected.subForest.map(visitTree)
expected.loc.getChild(2).get.toTree.drawTree
expected.levels.map(_.force).force
def visitPaths[A](tree: Tree[A]): List[A] = {
tree.subForest.flatMap { _ match {
case Tree.Node(root, children) =>
children.map { child =>
root :: visitPaths(child)
}
case leaf => List(leaf.rootLabel)
}}.toList.asInstanceOf[List[A]]
}
visitPaths(expected)
expected.subForest.foreach(println)
val allTreeLocs: Tree[TreeLoc[String]] = expected.loc.cojoin.toTree
// Getting the label of the focussed node from each TreeLoc restores the original tree
allTreeLocs.map(_.getLabel).drawTree
// Alternatively, we can get the path to root from each node
allTreeLocs.map(_.path).drawTree.println
leafPaths(expected).map(_.toList).toList
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment