summaryrefslogtreecommitdiff
path: root/docs/examples/patterns.scala
diff options
context:
space:
mode:
authormichelou <michelou@epfl.ch>2006-03-08 10:08:03 +0000
committermichelou <michelou@epfl.ch>2006-03-08 10:08:03 +0000
commitc44a597469fcd3549618348ad9808d923a1c9c2c (patch)
tree4f889d88e69cb0c8cd0a528d3f2f97529d60ff80 /docs/examples/patterns.scala
parent8a9572b96be66414598ca9d27b29f5f3867f2023 (diff)
downloadscala-c44a597469fcd3549618348ad9808d923a1c9c2c.tar.gz
scala-c44a597469fcd3549618348ad9808d923a1c9c2c.tar.bz2
scala-c44a597469fcd3549618348ad9808d923a1c9c2c.zip
adapted some more examples to Scala 2
Diffstat (limited to 'docs/examples/patterns.scala')
-rw-r--r--docs/examples/patterns.scala23
1 files changed, 12 insertions, 11 deletions
diff --git a/docs/examples/patterns.scala b/docs/examples/patterns.scala
index 34b0db96ae..e36149ad9c 100644
--- a/docs/examples/patterns.scala
+++ b/docs/examples/patterns.scala
@@ -1,12 +1,12 @@
-package examples;
+package examples
object patterns {
- trait Tree;
- case class Branch(left: Tree, right: Tree) extends Tree;
- case class Leaf(x: Int) extends Tree;
+ 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)));
+ 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)
@@ -14,12 +14,13 @@ object patterns {
}
def find[a,b](it: Iterator[Pair[a, b]], x: a): Option[b] = {
- var result: Option[b] = _;
- while (it.hasNext && result == null) {
- val Pair(x1, y) = it.next;
- if (x == x1) result = Some(y)
+ var result: Option[b] = None
+ var found = false
+ while (it.hasNext && !found) {
+ val Pair(x1, y) = it.next
+ if (x == x1) { found = true; result = Some(y) }
}
- if (result == null) None else result
+ result
}
def printFinds[a](xs: List[Pair[a, String]], x: a) =
@@ -29,7 +30,7 @@ object patterns {
}
def main(args: Array[String]): Unit = {
- Console.println("sum of leafs=" + sumLeaves(tree1));
+ Console.println("sum of leafs=" + sumLeaves(tree1))
printFinds(List(Pair(3, "three"), Pair(4, "four")), 4)
}
} \ No newline at end of file