summaryrefslogtreecommitdiff
path: root/src/library
diff options
context:
space:
mode:
authorPaul Phillips <paulp@improving.org>2010-12-03 06:17:23 +0000
committerPaul Phillips <paulp@improving.org>2010-12-03 06:17:23 +0000
commitcc1f6bca81c9f524952b4b42b45d8e36d8df6387 (patch)
tree5b2912a3c222616b8e23221e2871a04a9c1aac89 /src/library
parent979c57cd8732a2f9c9de066d532971986bcdff7d (diff)
downloadscala-cc1f6bca81c9f524952b4b42b45d8e36d8df6387.tar.gz
scala-cc1f6bca81c9f524952b4b42b45d8e36d8df6387.tar.bz2
scala-cc1f6bca81c9f524952b4b42b45d8e36d8df6387.zip
Parameterizes TraversableFactory.range so it ca...
Parameterizes TraversableFactory.range so it can be used with any integral type. Closes #3542, no review.
Diffstat (limited to 'src/library')
-rw-r--r--src/library/scala/collection/generic/TraversableFactory.scala15
-rw-r--r--src/library/scala/collection/immutable/Stream.scala8
-rw-r--r--src/library/scala/math/Equiv.scala7
3 files changed, 18 insertions, 12 deletions
diff --git a/src/library/scala/collection/generic/TraversableFactory.scala b/src/library/scala/collection/generic/TraversableFactory.scala
index c6f5ce4dde..55022349eb 100644
--- a/src/library/scala/collection/generic/TraversableFactory.scala
+++ b/src/library/scala/collection/generic/TraversableFactory.scala
@@ -195,7 +195,7 @@ abstract class TraversableFactory[CC[X] <: Traversable[X] with GenericTraversabl
* @param end the end value of the $coll (the first value NOT contained)
* @return a $coll with values `start, start + 1, ..., end - 1`
*/
- def range(start: Int, end: Int): CC[Int] = range(start, end, 1)
+ def range[T: Integral](start: T, end: T): CC[T] = range(start, end, implicitly[Integral[T]].one)
/** Produces a $coll containing equally spaced values in some integer interval.
* @param start the start value of the $coll
@@ -203,12 +203,15 @@ abstract class TraversableFactory[CC[X] <: Traversable[X] with GenericTraversabl
* @param step the difference between successive elements of the $coll (must be positive or negative)
* @return a $coll with values `start, start + step, ...` up to, but excluding `end`
*/
- def range(start: Int, end: Int, step: Int): CC[Int] = {
- if (step == 0) throw new IllegalArgumentException("zero step")
- val b = newBuilder[Int]
- b.sizeHint(Range.count(start, end, step, false))
+ def range[T: Integral](start: T, end: T, step: T): CC[T] = {
+ val num = implicitly[Integral[T]]
+ import num._
+
+ if (step == zero) throw new IllegalArgumentException("zero step")
+ val b = newBuilder[T]
+ b sizeHint immutable.NumericRange.count(start, end, step, false)
var i = start
- while (if (step < 0) end < i else i < end) {
+ while (if (step < zero) end < i else i < end) {
b += i
i += step
}
diff --git a/src/library/scala/collection/immutable/Stream.scala b/src/library/scala/collection/immutable/Stream.scala
index 04fe6225b4..3f16c2c020 100644
--- a/src/library/scala/collection/immutable/Stream.scala
+++ b/src/library/scala/collection/immutable/Stream.scala
@@ -675,9 +675,13 @@ object Stream extends SeqFactory[Stream] {
loop(0)
}
- override def range(start: Int, end: Int, step: Int): Stream[Int] =
- if (if (step < 0) start <= end else end <= start) Empty
+ override def range[T: Integral](start: T, end: T, step: T): Stream[T] = {
+ val num = implicitly[Integral[T]]
+ import num._
+
+ if (if (step < zero) start <= end else end <= start) Empty
else new Cons(start, range(start + step, end, step))
+ }
private[immutable] def filteredTail[A](stream: Stream[A], p: A => Boolean) = {
new Stream.Cons(stream.head, stream.tail filter p)
diff --git a/src/library/scala/math/Equiv.scala b/src/library/scala/math/Equiv.scala
index c09b4c577b..b7466b2e4e 100644
--- a/src/library/scala/math/Equiv.scala
+++ b/src/library/scala/math/Equiv.scala
@@ -39,10 +39,6 @@ trait LowPriorityEquiv {
self: Equiv.type =>
implicit def universalEquiv[T] : Equiv[T] = universal[T]
-
- implicit def comparatorToEquiv[T](cmp: Comparator[T]): Equiv[T] = new Equiv[T] {
- def equiv(x: T, y: T) = cmp.compare(x, y) == 0
- }
}
object Equiv extends LowPriorityEquiv {
@@ -52,6 +48,9 @@ object Equiv extends LowPriorityEquiv {
def universal[T] : Equiv[T] = new Equiv[T] {
def equiv(x: T, y: T) = x == y
}
+ def fromComparator[T](cmp: Comparator[T]): Equiv[T] = new Equiv[T] {
+ def equiv(x: T, y: T) = cmp.compare(x, y) == 0
+ }
def fromFunction[T](cmp: (T, T) => Boolean): Equiv[T] = new Equiv[T] {
def equiv(x: T, y: T) = cmp(x, y)
}