alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Scala example source code file (Equiv.scala)

This example Scala source code file (Equiv.scala) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Scala tags/keywords

anyref, boolean, boolean, comparator, equiv, equiv, lowpriorityequiv, s, s, t, t, util

The Scala Equiv.scala source code

/*                     __                                               *\
**     ________ ___   / /  ___     Scala API                            **
**    / __/ __// _ | / /  / _ |    (c) 2003-2011, LAMP/EPFL             **
**  __\ \/ /__/ __ |/ /__/ __ |    http://scala-lang.org/               **
** /____/\___/_/ |_/____/_/ | |                                         **
**                          |/                                          **
\*                                                                      */

package scala.math

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.  
 *
 *  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 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]
}

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 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)
  }
  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]]
}

Other Scala examples (source code examples)

Here is a short list of links related to this Scala Equiv.scala source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.