Skip to content

Instantly share code, notes, and snippets.

@yakkomajuri
Created July 25, 2020 12:44
Show Gist options
  • Save yakkomajuri/47737ce1f33867b3d7114f5824b5a158 to your computer and use it in GitHub Desktop.
Save yakkomajuri/47737ce1f33867b3d7114f5824b5a158 to your computer and use it in GitHub Desktop.
// If you define a Tree class like this:
sealed trait Tree[A]
case class Branch[A](l: Tree[A], r: Tree[A], v: A) extends Tree[A]
case class Leaf[A](v: A) extends Tree[A]
// Then summing up the tree can be done like this:
def sum(tree: Tree[Int]): Int = tree match {
case Branch(l, r, v) => sum(l) + sum(r) + v
case Leaf(v) => v
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment