diff options
author | Paul Phillips <paulp@improving.org> | 2009-08-27 02:18:46 +0000 |
---|---|---|
committer | Paul Phillips <paulp@improving.org> | 2009-08-27 02:18:46 +0000 |
commit | 8949b0f2557f016d9074eecceed0f47ad16e37ea (patch) | |
tree | 9d22c68e6492c9d7e6b7f0292af9afe73527e1b3 | |
parent | 7c20966e50a7eb25594c3efd1f0b01340f0bcd20 (diff) | |
download | scala-8949b0f2557f016d9074eecceed0f47ad16e37ea.tar.gz scala-8949b0f2557f016d9074eecceed0f47ad16e37ea.tar.bz2 scala-8949b0f2557f016d9074eecceed0f47ad16e37ea.zip |
Removed ComparableTraversableOps and NumericTra...
Removed ComparableTraversableOps and NumericTraversableOps in favor of
methods directly on Traversable. We can revisit this, but for the time
being I think it'd be wise to reduce the use of implicits wherever we
can easily do so.
-rw-r--r-- | src/library/scala/collection/Traversable.scala | 54 | ||||
-rw-r--r-- | src/library/scala/collection/generic/TraversableTemplate.scala | 32 |
2 files changed, 32 insertions, 54 deletions
diff --git a/src/library/scala/collection/Traversable.scala b/src/library/scala/collection/Traversable.scala index 9998e02242..1a8ca8a621 100644 --- a/src/library/scala/collection/Traversable.scala +++ b/src/library/scala/collection/Traversable.scala @@ -103,48 +103,6 @@ object Traversable extends TraversableFactory[Traversable] { self => def newBuilder[A]: Builder[A, Traversable[A]] = immutable.Traversable.newBuilder[A] - /** A wrapper class which adds `min` and `max` methods to iterables of an element type that has an Ordering. - */ - class ComparableTraversableOps[A](self: Traversable[A], cmp: Ordering[A]) { - - /** Returns the minimal element of the wrapped iterable `self` with respect to the given ordering `cmp` */ - def min: A = { - require(!self.isEmpty, "min(<empty>)") - var acc = self.head - for (x <- self) - if (cmp.lt(x, acc)) acc = x - acc - } - - /** Returns the maximal element of the wrapped iterable `self` with respect to the given ordering `cmp` */ - def max: A = { - require(!self.isEmpty, "max(<empty>)") - var acc = self.head - for (x <- self) - if (cmp.gt(x, acc)) acc = x - acc - } - } - - /** A wrapper class which adds `sum` and `product` methods to iterables of an element type that is `Numeric`. - */ - class NumericTraversableOps[A](self: Traversable[A], num: Numeric[A]) { - - /** Returns the sum of all elements of the wrapped iterable `self` with respect to the numeric operations in `num` */ - def sum: A = { - var acc = num.zero - for (x <- self) acc = num.plus(acc, x) - acc - } - - /** Returns the product of all elements of the wrapped iterable `self` with respect to the numeric operations in `num` */ - def product: A = { - var acc = num.one - for (x <- self) acc = num.times(acc, x) - acc - } - } - /** A wrapper class which adds `flatten` and `transpose` methods to iterables or iterable element type`. */ class TraversableTraversableOps[This <: Traversable[Traversable[A]], A](self: This) { @@ -192,18 +150,6 @@ object Traversable extends TraversableFactory[Traversable] { self => } } - /** Implicit wrapper conversion of iterables with elements admitting comparison. - * @see ComparableTraversableOps - */ - implicit def comparableTraversableWrapper[A](self: Traversable[A])(implicit cmp: Ordering[A]) = - new ComparableTraversableOps(self, cmp) - - /** Implicit wrapper conversion of iterables with numeric elements. - * @see NumericTraversableOps - */ - implicit def numericTraversableWrapper[A](self: Traversable[A])(implicit num: Numeric[A]) = - new NumericTraversableOps(self, num) - /** Implicit wrapper conversion of iterables with iterable elements. * @see TraversableTraversableOps */ diff --git a/src/library/scala/collection/generic/TraversableTemplate.scala b/src/library/scala/collection/generic/TraversableTemplate.scala index 79e050b976..2e808145cd 100644 --- a/src/library/scala/collection/generic/TraversableTemplate.scala +++ b/src/library/scala/collection/generic/TraversableTemplate.scala @@ -134,6 +134,38 @@ self => result } + /** Returns the sum of all elements with respect to the numeric operations in `num` */ + def sum[B >: A](implicit num: Numeric[B]): B = { + var acc = num.zero + for (x <- self) acc = num.plus(acc, x) + acc + } + + /** Returns the product of all elements with respect to the numeric operations in `num` */ + def product[B >: A](implicit num: Numeric[B]): B = { + var acc = num.one + for (x <- self) acc = num.times(acc, x) + acc + } + + /** Returns the minimal element with respect to the given ordering `cmp` */ + def min[B >: A](implicit cmp: Ordering[B]): A = { + require(!self.isEmpty, "min(<empty>)") + var acc = self.head + for (x <- self) + if (cmp.lt(x, acc)) acc = x + acc + } + + /** Returns the maximal element with respect to the given ordering `cmp` */ + def max[B >: A](implicit cmp: Ordering[B]): A = { + require(!self.isEmpty, "max(<empty>)") + var acc = self.head + for (x <- self) + if (cmp.gt(x, acc)) acc = x + acc + } + /** Returns true if this collection is known to have finite size. * This is the case if the collection type is strict, or if the * collection type is non-strict (e.g. it's a Stream), but all |