diff options
author | michelou <michelou@epfl.ch> | 2004-01-05 15:56:26 +0000 |
---|---|---|
committer | michelou <michelou@epfl.ch> | 2004-01-05 15:56:26 +0000 |
commit | ab7815a4ab25494efa0816083cb0cd41a14de788 (patch) | |
tree | 58e7b29d212cbef62ae12a70fec2b3896de6884c /sources/examples/patterns.scala | |
parent | 0adfc8d42abed2b4019a866672c497eb5d67e5b9 (diff) | |
download | scala-ab7815a4ab25494efa0816083cb0cd41a14de788.tar.gz scala-ab7815a4ab25494efa0816083cb0cd41a14de788.tar.bz2 scala-ab7815a4ab25494efa0816083cb0cd41a14de788.zip |
- added main functions.
Diffstat (limited to 'sources/examples/patterns.scala')
-rw-r--r-- | sources/examples/patterns.scala | 23 |
1 files changed, 15 insertions, 8 deletions
diff --git a/sources/examples/patterns.scala b/sources/examples/patterns.scala index 3e4d5a5826..34b0db96ae 100644 --- a/sources/examples/patterns.scala +++ b/sources/examples/patterns.scala @@ -1,8 +1,10 @@ -module patterns { +package examples; - trait Tree {} - case class Branch(left: Tree, right: Tree) extends Tree {} - case class Leaf(x: Int) extends Tree {} +object patterns { + + trait Tree; + case class Branch(left: Tree, right: Tree) extends Tree; + case class Leaf(x: Int) extends Tree; val tree1 = Branch(Branch(Leaf(1), Leaf(2)), Branch(Leaf(3), Leaf(4))); @@ -12,17 +14,22 @@ module patterns { } def find[a,b](it: Iterator[Pair[a, b]], x: a): Option[b] = { - var result: Option[b] = None(); - while (it.hasNext && result.isNone) { + var result: Option[b] = _; + while (it.hasNext && result == null) { val Pair(x1, y) = it.next; if (x == x1) result = Some(y) } - result + if (result == null) None else result } def printFinds[a](xs: List[Pair[a, String]], x: a) = find(xs.elements, x) match { case Some(y) => System.out.println(y) - case None() => System.out.println("no match") + case None => System.out.println("no match") } + + def main(args: Array[String]): Unit = { + Console.println("sum of leafs=" + sumLeaves(tree1)); + printFinds(List(Pair(3, "three"), Pair(4, "four")), 4) + } }
\ No newline at end of file |