From cc1f6bca81c9f524952b4b42b45d8e36d8df6387 Mon Sep 17 00:00:00 2001 From: Paul Phillips Date: Fri, 3 Dec 2010 06:17:23 +0000 Subject: Parameterizes TraversableFactory.range so it ca... Parameterizes TraversableFactory.range so it can be used with any integral type. Closes #3542, no review. --- .../scala/collection/generic/TraversableFactory.scala | 15 +++++++++------ src/library/scala/collection/immutable/Stream.scala | 8 ++++++-- src/library/scala/math/Equiv.scala | 7 +++---- 3 files changed, 18 insertions(+), 12 deletions(-) (limited to 'src/library') 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) } -- cgit v1.2.3