diff options
author | Paul Phillips <paulp@improving.org> | 2010-11-29 22:59:28 +0000 |
---|---|---|
committer | Paul Phillips <paulp@improving.org> | 2010-11-29 22:59:28 +0000 |
commit | 4ec7f11a799444c3758e94b3fdf9fa5c26330577 (patch) | |
tree | 05a6a144c694fc52f6855884e1a94b5f61bfcfd1 /src | |
parent | cbcf3f505144297ad197d0086ea7a4c4f1fbb598 (diff) | |
download | scala-4ec7f11a799444c3758e94b3fdf9fa5c26330577.tar.gz scala-4ec7f11a799444c3758e94b3fdf9fa5c26330577.tar.bz2 scala-4ec7f11a799444c3758e94b3fdf9fa5c26330577.zip |
Fleshed out Equiv[T] so that it can be used in ...
Fleshed out Equiv[T] so that it can be used in a manner similar to
Ordering[T]. No review.
Diffstat (limited to 'src')
-rw-r--r-- | src/library/scala/math/Equiv.scala | 69 |
1 files changed, 43 insertions, 26 deletions
diff --git a/src/library/scala/math/Equiv.scala b/src/library/scala/math/Equiv.scala index a45b51af49..c09b4c577b 100644 --- a/src/library/scala/math/Equiv.scala +++ b/src/library/scala/math/Equiv.scala @@ -6,40 +6,57 @@ ** |/ ** \* */ - - package scala.math -/** <p> - * A trait for representing equivalence relations. It is important to - * distinguish between a type that can be compared for equality or - * equivalence and a representation of equivalence on some type. This - * trait is for representing the latter. - * </p> - * <p> - * An <a href="http://en.wikipedia.org/wiki/Equivalence_relation">equivalence - * relation</a> is a binary relation on a type. This relation is exposed as - * the <code>equiv</code> method of the <code>Equiv</code> trait. This - * relation must be: - * </p> - * <ul> - * <li>reflexive: <code>equiv(x, x) == true</code>, for any <code>x</code> of - * type <code>T</code>.</li> - * <li>symmetric: <code>equiv(x, y) == equiv(y, x)</code>, for any - * <code>x</code> and <code>y</code> of type <code>T</code>.</li> - * <li>transitive: if <code>equiv(x, y) == true</code> and <code>equiv(y, z) == true</code> - * then <code>equiv(x, z) == true</code>, for any <code>x</code>, <code>y</code>, - * and <code>z</code> of type <code>T</code>.</li> - * </ul> +import java.util.Comparator + +/** A trait for representing equivalence relations. It is important to + * distinguish between a type that can be compared for equality or + * equivalence and a representation of equivalence on some type. This + * trait is for representing the latter. * - * @author Geoffrey Washburn + * An <a href="http://en.wikipedia.org/wiki/Equivalence_relation">equivalence + * relation</a> is a binary relation on a type. This relation is exposed as + * the `equiv` method of the `Equiv` trait. The relation must be: + * + * 1. reflexive: equiv(x, x) == true for any x of type T. + * 2. symmetric: equiv(x, y) == equiv(y, x) for any x and y of type T. + * 3. transitive: if equiv(x, y) == true and equiv(y, z) == true, then + * equiv(x, z) == true for any x, y, and z of type T. + * + * @author Geoffrey Washburn, Paul Phillips * @version 1.0, 2008-04-03 * @since 2.7 */ trait Equiv[T] { - /** Returns <code>true</code> iff <code>x</code> is equivalent to - * <code>y</code>. + /** Returns true iff x is equivalent to y. */ def equiv(x: T, y: T): Boolean } + +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 { + def reference[T <: AnyRef] : Equiv[T] = new Equiv[T] { + def equiv(x: T, y: T) = x eq y + } + def universal[T] : Equiv[T] = new Equiv[T] { + def equiv(x: T, y: T) = x == y + } + def fromFunction[T](cmp: (T, T) => Boolean): Equiv[T] = new Equiv[T] { + def equiv(x: T, y: T) = cmp(x, y) + } + def by[T, S: Equiv](f: T => S): Equiv[T] = + fromFunction((x, y) => implicitly[Equiv[S]].equiv(f(x), f(y))) + + def apply[T: Equiv] : Equiv[T] = implicitly[Equiv[T]] +} |