ATuin

Untitled

Oct 12th, 2012
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 0.53 KB | None | 0 0
  1. object Test {
  2.  sealed abstract class Tree[+T] {
  3.   def leafCount: Int
  4.  }
  5.  
  6.  case class Node[+T](value: T, left: Tree[T], right: Tree[T]) extends Tree[T] {
  7.   def leafCount: Int = (left, right) match {
  8.     case (End, End) => 1
  9.     case _          => left.leafCount + right.leafCount
  10.   }
  11.  }
  12.  
  13.  case object End extends Tree[Nothing] {
  14.   def leafCount: Int = 0
  15.  }
  16.  
  17.  object Node {
  18.     def apply[T](value: T): Node[T] = Node(value, End, End)  
  19.   }
  20.  
  21.  def main(args:Array[String]) = {
  22.    println(Node('x', Node('x'), End).leafCount)
  23. }
Advertisement
Add Comment
Please, Sign In to add comment