diff options
author | Paul Phillips <paulp@improving.org> | 2009-08-17 20:42:48 +0000 |
---|---|---|
committer | Paul Phillips <paulp@improving.org> | 2009-08-17 20:42:48 +0000 |
commit | 22fcda03418bafc85f6fd6f38f2478677b99c932 (patch) | |
tree | 86b6794f3a0dcabb591d4ff077e19f8008434685 /src | |
parent | d0ca666b758fa9f6060361848dc410b81e5cb1ed (diff) | |
download | scala-22fcda03418bafc85f6fd6f38f2478677b99c932.tar.gz scala-22fcda03418bafc85f6fd6f38f2478677b99c932.tar.bz2 scala-22fcda03418bafc85f6fd6f38f2478677b99c932.zip |
Fixed both versions of Iterator.iterate, which ...
Fixed both versions of Iterator.iterate, which were a bit off, and gave
them type parameters.
Before: Iterator.iterate(5, 10)(_ + 1) toList
res0: List[Int] = List(6, 6, 6, 6, 6, 6, 6, 6, 6, 6)
After: Iterator.iterate(5L)(_ + 1) take 10 toList
res1: List[Long] = List(5, 6, 7, 8, 9, 10, 11, 12, 13, 14)
Also, I deprecated the two argument version because
def iterate[T](start: T, len: Int)(f: T => T)
is identical to
iterate(start)(f) take len
Diffstat (limited to 'src')
-rw-r--r-- | src/library/scala/collection/Iterator.scala | 15 |
1 files changed, 4 insertions, 11 deletions
diff --git a/src/library/scala/collection/Iterator.scala b/src/library/scala/collection/Iterator.scala index 1335f37e8f..dabbc86cbc 100644 --- a/src/library/scala/collection/Iterator.scala +++ b/src/library/scala/collection/Iterator.scala @@ -109,14 +109,8 @@ object Iterator { * @param f the function that's repeatedly applied * @return the iterator returning `len` values in the sequence `start, f(start), f(f(start)), ...` */ - def iterate(start: Int, len: Int)(f: Int => Int) = new Iterator[Int] { - private var acc = start - private var i = 0 - def hasNext: Boolean = i < len - def next(): Int = - if (hasNext) { val result = f(acc); i += 1; result } - else empty.next() - } + @deprecated("Use `iterate(start)(f) take len' instead") + def iterate[T](start: T, len: Int)(f: T => T): Iterator[T] = iterate(start)(f) take len /** An infinite iterator that repeatedly applies a given function to a start value. * @@ -124,11 +118,10 @@ object Iterator { * @param f the function that's repeatedly applied * @return the iterator returning the infinite sequence of values `start, f(start), f(f(start)), ...` */ - def iterate(start: Int)(f: Int => Int) = new Iterator[Int] { + def iterate[T](start: T)(f: T => T): Iterator[T] = new Iterator[T] { private var acc = start - private var i = 0 def hasNext: Boolean = true - def next(): Int = { val result = f(acc); i += 1; result } + def next(): T = { val res = acc ; acc = f(acc) ; res } } /** An infinite-length iterator which returns successive values from some start value. |