summaryrefslogtreecommitdiff
path: root/sources
diff options
context:
space:
mode:
authormichelou <michelou@epfl.ch>2004-05-10 15:15:10 +0000
committermichelou <michelou@epfl.ch>2004-05-10 15:15:10 +0000
commit92659885e30a597497c3859c19ca1614656ccec7 (patch)
tree2b1c39dd1843431cc273f9cd1f7b4e262546df27 /sources
parent1389f3407e3783855bfb76f048d78c566ef37b68 (diff)
downloadscala-92659885e30a597497c3859c19ca1614656ccec7.tar.gz
scala-92659885e30a597497c3859c19ca1614656ccec7.tar.bz2
scala-92659885e30a597497c3859c19ca1614656ccec7.zip
- added overloaded methods 'range' with step va...
- added overloaded methods 'range' with step value/function.
Diffstat (limited to 'sources')
-rw-r--r--sources/scala/Stream.scala50
1 files changed, 46 insertions, 4 deletions
diff --git a/sources/scala/Stream.scala b/sources/scala/Stream.scala
index f0c1415f9f..534d2842f2 100644
--- a/sources/scala/Stream.scala
+++ b/sources/scala/Stream.scala
@@ -1,6 +1,6 @@
/* __ *\
** ________ ___ / / ___ Scala API **
-** / __/ __// _ | / / / _ | (c) 2003, LAMP/EPFL **
+** / __/ __// _ | / / / _ | (c) 2003-2004, LAMP/EPFL **
** __\ \/ /__/ __ |/ /__/ __ | **
** /____/\___/_/ |_/____/_/ | | **
** |/ **
@@ -41,9 +41,51 @@ object Stream {
else empty;
}
- def range(start: int, end: int): Stream[int] =
- if (start >= end) empty
- else cons(start, range(start + 1, end));
+ /** 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> &lt; 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>.
+ */
+ 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> &lt; 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>.
+ */
+ def range(start: Int, end: Int, step: Int): Stream[Int] = {
+ def loop(lo: Int): Stream[Int] =
+ if (lo >= end) empty
+ else cons(lo, loop(lo + step));
+ 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> &lt; 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>.
+ */
+ def range(start: Int, end: Int, step: Int => Int): Stream[Int] = {
+ def loop(lo: Int): Stream[Int] =
+ if (lo >= end) empty
+ else cons(lo, loop(step(lo)));
+ loop(start)
+ }
}
trait Stream[+a] extends Seq[a] {