diff options
author | michelou <michelou@epfl.ch> | 2004-06-09 14:12:18 +0000 |
---|---|---|
committer | michelou <michelou@epfl.ch> | 2004-06-09 14:12:18 +0000 |
commit | 0602ac4d0bad99aee1557fc1137199bf1d2c06d7 (patch) | |
tree | 1b359a8d7ca78531440b6b1a65811e6555daa903 | |
parent | b33917d779989a8b66a25ed45921584758c9d7bb (diff) | |
download | scala-0602ac4d0bad99aee1557fc1137199bf1d2c06d7.tar.gz scala-0602ac4d0bad99aee1557fc1137199bf1d2c06d7.tar.bz2 scala-0602ac4d0bad99aee1557fc1137199bf1d2c06d7.zip |
- added code example.
-rw-r--r-- | sources/scala/Enumeration.scala | 9 | ||||
-rw-r--r-- | sources/scala/Stream.scala | 78 |
2 files changed, 57 insertions, 30 deletions
diff --git a/sources/scala/Enumeration.scala b/sources/scala/Enumeration.scala index 15b24b5ac7..2aeaaf0cc0 100644 --- a/sources/scala/Enumeration.scala +++ b/sources/scala/Enumeration.scala @@ -16,16 +16,13 @@ import scala.collection.mutable._; * <code>enum</code> construct found in C-like languages like C++ or Java. * Here is an example:</p> * <pre> - * object Main with Application { + * <b>object</b> Main <b>with</b> Application { * - * val days = List("Monday", "Tuesday", "Wednesday", "Thuesday", - * "Friday", "Saturday", "Sunday"); - * - * object WeekDays extends Enumeration(0, days:_*) { + * <b>object</b> WeekDays <b>extends</b> Enumeration { * val Mon, Tue, Wed, Thu, Fri, Sat, Sun = Value * } * - * def isWorkingDay(d: WeekDays.Value) = + * <b>def</b> isWorkingDay(d: WeekDays.Value) = * ! (d == WeekDays.Sat || d == WeekDays.Sun); * * WeekDays filter (isWorkingDay) foreach { d => System.out.println(d) } diff --git a/sources/scala/Stream.scala b/sources/scala/Stream.scala index 534d2842f2..2766548ca3 100644 --- a/sources/scala/Stream.scala +++ b/sources/scala/Stream.scala @@ -9,7 +9,13 @@ package scala; - +/** + * The object <code>Stream</code> provides helper functions + * to manipulate streams. + * + * @author Martin Odersky, Matthias Zenger + * @version 1.1 08/08/03 + */ object Stream { val empty: Stream[All] = new Stream[All] { @@ -41,27 +47,29 @@ object Stream { else empty; } - /** Create a stream with element values - * <code>v<sub>n+1</sub> = v<sub>n</sub> + 1</code> - * where <code>v<sub>0</sub> = start</code> - * and <code>v<sub>i</sub> < end</code>. + /** + * Create a stream with element values + * <code>v<sub>n+1</sub> = v<sub>n</sub> + 1</code> + * where <code>v<sub>0</sub> = start</code> + * and <code>v<sub>i</sub> < end</code>. * - * @param start the start value of the stream - * @param end the end value of the stream - * @return the stream starting at value <code>start</code>. + * @param start the start value of the stream + * @param end the end value of the stream + * @return the stream starting at value <code>start</code>. */ def range(start: Int, end: Int): Stream[Int] = range(start, end, 1); - /** Create a stream with element values - * <code>v<sub>n+1</sub> = v<sub>n</sub> + step</code> - * where <code>v<sub>0</sub> = start</code> - * and <code>v<sub>i</sub> < end</code>. + /** + * Create a stream with element values + * <code>v<sub>n+1</sub> = v<sub>n</sub> + step</code> + * where <code>v<sub>0</sub> = start</code> + * and <code>v<sub>i</sub> < end</code>. * - * @param start the start value of the stream - * @param end the end value of the stream - * @param step the increment value of the stream - * @return the stream starting at value <code>start</code>. + * @param start the start value of the stream + * @param end the end value of the stream + * @param step the increment value of the stream + * @return the stream starting at value <code>start</code>. */ def range(start: Int, end: Int, step: Int): Stream[Int] = { def loop(lo: Int): Stream[Int] = @@ -70,15 +78,16 @@ object Stream { loop(start) } - /** Create a stream with element values - * <code>v<sub>n+1</sub> = step(v<sub>n</sub>)</code> - * where <code>v<sub>0</sub> = start</code> - * and <code>v<sub>i</sub> < end</code>. + /** + * Create a stream with element values + * <code>v<sub>n+1</sub> = step(v<sub>n</sub>)</code> + * where <code>v<sub>0</sub> = start</code> + * and <code>v<sub>i</sub> < end</code>. * - * @param start the start value of the stream - * @param end the end value of the stream - * @param step the increment function of the stream - * @return the stream starting at value <code>start</code>. + * @param start the start value of the stream + * @param end the end value of the stream + * @param step the increment function of the stream + * @return the stream starting at value <code>start</code>. */ def range(start: Int, end: Int, step: Int => Int): Stream[Int] = { def loop(lo: Int): Stream[Int] = @@ -88,6 +97,27 @@ object Stream { } } +/** + * <p>The class <code>Stream</code> implements lazy lists where elements + * are only evaluated when they are needed. Here is an example:</p> + * <pre> + * <b>object</b> Main <b>with</b> Application { + * + * <b>def</b> from(n: Int): Stream[Int] = + * Stream.cons(n, from(n + 1)); + * + * <b>def</b> sieve(s: Stream[Int]): Stream[Int] = + * Stream.cons(s.head, sieve(s.tail filter { x => x % s.head != 0 })); + * + * <b>def</b> primes = sieve(from(2)); + * + * primes take 10 print + * } + * </pre> + * + * @author Martin Odersky, Matthias Zenger + * @version 1.1 08/08/03 + */ trait Stream[+a] extends Seq[a] { def isEmpty: Boolean; |