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 | |
parent | 0adfc8d42abed2b4019a866672c497eb5d67e5b9 (diff) | |
download | scala-ab7815a4ab25494efa0816083cb0cd41a14de788.tar.gz scala-ab7815a4ab25494efa0816083cb0cd41a14de788.tar.bz2 scala-ab7815a4ab25494efa0816083cb0cd41a14de788.zip |
- added main functions.
Diffstat (limited to 'sources')
-rw-r--r-- | sources/examples/futures.scala | 15 | ||||
-rw-r--r-- | sources/examples/iterators.scala | 7 | ||||
-rw-r--r-- | sources/examples/patterns.scala | 23 |
3 files changed, 30 insertions, 15 deletions
diff --git a/sources/examples/futures.scala b/sources/examples/futures.scala index 1e218351af..905d360612 100644 --- a/sources/examples/futures.scala +++ b/sources/examples/futures.scala @@ -1,12 +1,17 @@ package examples; + import concurrent.ops._; -module futures { + +object futures { def someLengthyComputation = 1; def anotherLengthyComputation = 2; def f(x: Int) = x + x; def g(x: Int) = x * x; - val x = future(someLengthyComputation); - anotherLengthyComputation; - val y = f(x()) + g(x()); - System.out.println(y); + + def main(args: Array[String]): Unit = { + val x = future(someLengthyComputation); + anotherLengthyComputation; + val y = f(x()) + g(x()); + Console.println(y) + } }
\ No newline at end of file diff --git a/sources/examples/iterators.scala b/sources/examples/iterators.scala index 958934e65e..7936e413da 100644 --- a/sources/examples/iterators.scala +++ b/sources/examples/iterators.scala @@ -1,3 +1,5 @@ +package examples; + object iterators { def printArray(xs: Array[Double]) = @@ -10,10 +12,11 @@ object iterators { .map{case Pair(x, i) => i} def main(args: Array[String]) = { - val xs = List(6, 2, 8, 5, 1); + val xs: List[Double] = List(6, 2, 8, 5, 1); val ar = xs.copyToArray(new Array[Double](xs.length), 0); printArray(ar); - findGreater(ar, 3.0) foreach { x => Console.print(x); }; + Console.println("Elements greater than 3.0:"); + findGreater(ar, 3.0) foreach { x => Console.println(ar(x)); } } } 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 |