diff options
author | Martin Odersky <odersky@gmail.com> | 2003-03-03 14:33:53 +0000 |
---|---|---|
committer | Martin Odersky <odersky@gmail.com> | 2003-03-03 14:33:53 +0000 |
commit | 6749e5dd658522cb63600021a9ee5a86f911cfeb (patch) | |
tree | a22d4bf7f2bf71b5775418dfddaa31a1640313d1 /sources/examples/patterns.scala | |
parent | e1fb3fb655a067039870016b3a47e2305d692d98 (diff) | |
download | scala-6749e5dd658522cb63600021a9ee5a86f911cfeb.tar.gz scala-6749e5dd658522cb63600021a9ee5a86f911cfeb.tar.bz2 scala-6749e5dd658522cb63600021a9ee5a86f911cfeb.zip |
*** empty log message ***
Diffstat (limited to 'sources/examples/patterns.scala')
-rw-r--r-- | sources/examples/patterns.scala | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/sources/examples/patterns.scala b/sources/examples/patterns.scala new file mode 100644 index 0000000000..3e4d5a5826 --- /dev/null +++ b/sources/examples/patterns.scala @@ -0,0 +1,28 @@ +module 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))); + + def sumLeaves(t: Tree): Int = t match { + case Branch(l, r) => sumLeaves(l) + sumLeaves(r) + case Leaf(x) => x + } + + def find[a,b](it: Iterator[Pair[a, b]], x: a): Option[b] = { + var result: Option[b] = None(); + while (it.hasNext && result.isNone) { + val Pair(x1, y) = it.next; + if (x == x1) result = Some(y) + } + 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") + } +}
\ No newline at end of file |