diff options
author | Sean McDirmid <sean.mcdirmid@gmail.com> | 2007-05-22 13:56:08 +0000 |
---|---|---|
committer | Sean McDirmid <sean.mcdirmid@gmail.com> | 2007-05-22 13:56:08 +0000 |
commit | 9ff91b59862d7b734ae2515411c544e706637351 (patch) | |
tree | 7d384f8cba050b9d90303bb89966fb84cf138606 | |
parent | 02a1f9afa99ec7af37b0ce0bb02ec6159b93fa84 (diff) | |
download | scala-9ff91b59862d7b734ae2515411c544e706637351.tar.gz scala-9ff91b59862d7b734ae2515411c544e706637351.tar.bz2 scala-9ff91b59862d7b734ae2515411c544e706637351.zip |
Added by method to Range, made range configurable.
-rw-r--r-- | src/library/scala/Iterator.scala | 5 | ||||
-rw-r--r-- | src/library/scala/Range.scala | 16 |
2 files changed, 13 insertions, 8 deletions
diff --git a/src/library/scala/Iterator.scala b/src/library/scala/Iterator.scala index f019357982..70f0b9516b 100644 --- a/src/library/scala/Iterator.scala +++ b/src/library/scala/Iterator.scala @@ -126,10 +126,7 @@ object Iterator { * @param step the increment value of the iterator (must be positive or negative) * @return the iterator with values in range <code>[start;end)</code>. */ - def range(start: Int, end: Int, step: Int): Range = - if (start >= end && step > 0) new Range(0, 0, 1) - else if (start <= end && step < 0) new Range(0, 0, 1) - else new Range(start, end, step) + def range(start: Int, end: Int, step: Int): Range = new Range(start, end, step) /** Create an iterator with elements * <code>e<sub>n+1</sub> = step(e<sub>n</sub>)</code> diff --git a/src/library/scala/Range.scala b/src/library/scala/Range.scala index 2fdef9a085..7da93d4afb 100644 --- a/src/library/scala/Range.scala +++ b/src/library/scala/Range.scala @@ -28,15 +28,17 @@ import Predef._ * @version 1.0, 01/05/2007 */ class Range(val start: Int, val end: Int, val step: Int) extends BufferedIterator[Int] { - assert(step != 0) - assert(if (step > 0) end >= start else end <= start) + if (step == 0) throw new Predef.IllegalArgumentException private var jdx = 0 + /** create a new range with the start and end values of this range and a new <code>step</code> */ + def by(step : Int) = new Range(start, end, step) + override def peekList(sz : Int) : Seq[Int] = return new RandomAccessSeq.Projection[Int] { def length = Range.this.length - jdx def apply(idx : Int) = Range.this.apply(jdx + idx) } - override def hasNext = jdx < length + override def hasNext = if (step == 0) true else jdx < length override def next = { val ret = apply(jdx) jdx = jdx + 1 @@ -46,7 +48,13 @@ class Range(val start: Int, val end: Int, val step: Int) extends BufferedIterato def length = Range.this.length def apply(idx : Int) = Range.this.apply(idx) } - def length = { + + + def length : Int = { + if (this.step == 0) throw new Predef.UnsupportedOperationException + if (start < end && this.step < 0) return 0 + if (start > end && this.step > 0) return 0 + val base = if (start < end) end - start else start - end assert(base >= 0) |