Comparer

com.phasmidsoftware.comparer.Comparer
See theComparer companion object
trait Comparer[T] extends T => T => Comparison

Type class trait Comparer[T].

The behavior of this trait is that it is a lazy comparer of two T instances, presented as two curried parameters, the inner (first) and the outer (second). In all (curried) comparisons, the outer instance is compared with the inner instance. The result of the comparison is a Comparison object.

This class extends the function T => T => Comparison, i.e., a curried function. This is more appropriate here because the two T values being compared are not related in any way--they do not form part of something. Furthermore, using a curried function allows us to yield a partially applied function, which is a closure on just one of the comparands.

A Comparer for a case class can be derived, comparing the fields in order from most to least significant:

 case class Date(year: Int, month: Int, day: Int) derives Comparer

Type parameters

T

the underlying type of the comparer. That's to say the type this Comparer knows about.

Attributes

Companion
object
Graph
Supertypes
trait T => T => Comparison
class Object
trait Matchable
class Any

Members list

Value members

Concrete methods

def !=(t1: T)(t2: T): Boolean

Method to yield a Boolean from this Comparer, given two curried Ts.

Method to yield a Boolean from this Comparer, given two curried Ts.

Value parameters

t1

the first value of T.

t2

the second value of T.

Attributes

Returns

a Boolean that is true if the two Ts do not compare as the same.

def !=(tt: (T, T)): Boolean

Method to yield a Boolean from this Comparer, given two tupled Ts.

Method to yield a Boolean from this Comparer, given two tupled Ts.

Value parameters

tt

the tuple of Ts.

Attributes

Returns

a Boolean that is true if the two Ts do not compare as the same.

def :|[U : Comparer](lens: T => U): Comparer[T]

Method to compose this Comparer[T] with a Comparer[U] that is formed from the given "lens" function. See, for example, the definition of the Comparer in object DateF (in ComparerSpec). Of all the methods which result in a new Comparer, this one is probably the most useful.

Method to compose this Comparer[T] with a Comparer[U] that is formed from the given "lens" function. See, for example, the definition of the Comparer in object DateF (in ComparerSpec). Of all the methods which result in a new Comparer, this one is probably the most useful.

The resulting Comparer[T] is formed by using orElse to compose this Comparer[T] with a Comparer[T] that: is formed by using the "lens" function lens to snap the given Comparer[U] into a Comparer[T].

This method is used primarily when chaining together several Comparers, each of which is derived from the given function by invoking snap on a given Comparer with the specified function. The initial value is typically provided by the "same" method of Comparer's companion object.

Type parameters

U

the underlying type of the given comparer.

Value parameters

lens

a function that takes a T (the underlying type of this and the result) and returns a U.

Attributes

Returns

a Comparer[T] which is composed of this and the unmapped form of comparer.

def :|![U : Comparer](lens: T => U): Comparer[T]

Method to compose this Comparer[T] with a "lens" function that operates on a T. See, for example, the definition of the Comparer in object DateF (in ComparerSpec).

Method to compose this Comparer[T] with a "lens" function that operates on a T. See, for example, the definition of the Comparer in object DateF (in ComparerSpec).

The resulting Comparer[T] is formed by using orElseNot to compose this Comparer[T] with a Comparer[T] that: is formed by using the "lens" function lens to snap the given Comparer[U] into a Comparer[T].

This method is used primarily when chaining together several Comparers, each of which is derived from the given function by invoking snap on a given Comparer, with the specified function. The initial value is typically provided by the "same" method of Comparer's companion object.

Type parameters

U

the underlying type of the given comparer.

Value parameters

lens

a function that takes a T (the underlying type of this and the result) and returns a U.

Attributes

Returns

a Comparer[T] which is composed of this and the unmapped form of comparer.

def <(t1: T)(t2: T): Boolean

Method to yield a Boolean from this Comparer, given two curried Ts.

Method to yield a Boolean from this Comparer, given two curried Ts.

Value parameters

t1

the first value of T.

t2

the second value of T.

Attributes

Returns

a Boolean that is true if the left T is less than the right T.

def <(tt: (T, T)): Boolean

Method to yield a Boolean from this Comparer, given two tupled Ts.

Method to yield a Boolean from this Comparer, given two tupled Ts.

Value parameters

tt

the tuple of Ts.

Attributes

Returns

a Boolean that is true if the left T is less than the right T.

def <=(t1: T)(t2: T): Boolean

Method to yield a Boolean from this Comparer, given two curried Ts.

Method to yield a Boolean from this Comparer, given two curried Ts.

Value parameters

t1

the first value of T.

t2

the second value of T.

Attributes

Returns

a Boolean that is true if the left T is less than or equal to the right T.

def <=(tt: (T, T)): Boolean

Method to yield a Boolean from this Comparer, given two tupled Ts.

Method to yield a Boolean from this Comparer, given two tupled Ts.

Value parameters

tt

the tuple of Ts.

Attributes

Returns

a Boolean that is true if the left T is less than or equal to the right T.

def ==(t1: T)(t2: T): Boolean

Method to yield a Boolean from this Comparer, given two curried Ts.

Method to yield a Boolean from this Comparer, given two curried Ts.

Value parameters

t1

the first value of T.

t2

the second value of T.

Attributes

Returns

a Boolean that is true if the Ts compare as the same.

def ==(tt: (T, T)): Boolean

Method to yield a Boolean from this Comparer, given two tupled Ts.

Method to yield a Boolean from this Comparer, given two tupled Ts.

Value parameters

tt

the tuple of Ts.

Attributes

Returns

a Boolean that is true if the Ts compare as the same.

def >(t1: T)(t2: T): Boolean

Method to yield a Boolean from this Comparer, given two curried Ts.

Method to yield a Boolean from this Comparer, given two curried Ts.

Value parameters

t1

the first value of T.

t2

the second value of T.

Attributes

Returns

a Boolean that is true if the left T is greater than the right T.

def >(tt: (T, T)): Boolean

Method to yield a Boolean from this Comparer, given two tupled Ts. NOTE: When you use these tupled forms, the compiler doesn't need an extra set of parentheses.

Method to yield a Boolean from this Comparer, given two tupled Ts. NOTE: When you use these tupled forms, the compiler doesn't need an extra set of parentheses.

Value parameters

tt

the tuple of Ts. Try saying that a few times!

Attributes

Returns

a Boolean that is true if the left T is greater than the right T.

def >=(t1: T)(t2: T): Boolean

Method to yield a Boolean from this Comparer, given two curried Ts.

Method to yield a Boolean from this Comparer, given two curried Ts.

Value parameters

t1

the first value of T.

t2

the second value of T.

Attributes

Returns

a Boolean that is true if the left T is greater than or equal to the right T.

def >=(tt: (T, T)): Boolean

Method to yield a Boolean from this Comparer, given two tupled Ts.

Method to yield a Boolean from this Comparer, given two tupled Ts.

Value parameters

tt

the tuple of Ts.

Attributes

Returns

a Boolean that is true if the left T is greater than or equal to the right T.

def compare(tt: (T, T)): Comparison

Method to yield a Comparison from a tuple of Ts. This, then, invokes the tupled version of apply. This is essentially the same as the tupled method where the two input parameters are both Ts.

Method to yield a Comparison from a tuple of Ts. This, then, invokes the tupled version of apply. This is essentially the same as the tupled method where the two input parameters are both Ts.

Value parameters

tt

a tuple (T,T).

Attributes

Returns

a Comparison

infix def compose[U](uc: => Comparer[U]): Comparer[(T, U)]

A method to compose this Comparer with another Comparer of a different underlying type.

A method to compose this Comparer with another Comparer of a different underlying type.

Type parameters

U

the underlying type of uc.

Value parameters

uc

a Comparer[U].

Attributes

Returns

a Comparer of tuples each comprising a T and a U.

def invert: Comparer[T]

Method to invert the sense of a Comparer.

Method to invert the sense of a Comparer.

Attributes

Returns

a Comparer[T] which, given the same tuple of Ts, yields the complementary Comparison to this Comparer.

infix def orElse(tc: => Comparer[T]): Comparer[T]

Compose this Comparer with another Comparer of the same underlying type. If the result of applying this Comparer is Same, then Comparer tc is invoked.

Compose this Comparer with another Comparer of the same underlying type. If the result of applying this Comparer is Same, then Comparer tc is invoked.

Value parameters

tc

the other Comparer (lazily evaluated).

Attributes

Returns

the result of applying this Comparer unless it yields Same, in which case we invoke the other Comparer.

infix def orElseNot(tc: => Comparer[T]): Comparer[T]

Compose this Comparer with another Comparer of the same underlying type. If the result of applying this Comparer is Same, then Comparer tc is inverted and then invoked.

Compose this Comparer with another Comparer of the same underlying type. If the result of applying this Comparer is Same, then Comparer tc is inverted and then invoked.

Value parameters

tc

the other Comparer (lazily evaluated) which is evaluated in the contrary sense.

Attributes

Returns

the result of applying this Comparer unless it yields Same, in which case we invoke the inverse of the other Comparer.

infix def snap[U](lens: U => T): Comparer[U]

Method to apply a lens function U=>T to this Comparer[T], resulting in a Comparer[U]. The U values that are the inputs to the resulting Comparer, are passed to the lens function to yield a suitable pair of T values which can then be passed into this Comparer.

Method to apply a lens function U=>T to this Comparer[T], resulting in a Comparer[U]. The U values that are the inputs to the resulting Comparer, are passed to the lens function to yield a suitable pair of T values which can then be passed into this Comparer.

I originally considered this the infamous "unMap" method which is part of an un-monad. Observe that the lens function is U=>T instead of T=>U which would be the parameter of a monadic map method. In case you're wondering, an un-monad is the wrappee as opposed to the wrapper (which would be a monad). However, I think it makes slightly more sense to call this snap because it creates something (kind of like a picture) by using a lens.

Type parameters

U

the underlying type of the returned Comparer.

Value parameters

lens

a function that takes a U and returns a T.

Attributes

Returns

a Comparer[U].

def toOrdering: Ordering[T]

Method to convert this Comparer[T] into an Ordering[T] which can then be used for more typical Java/Scala-style comparisons.

Method to convert this Comparer[T] into an Ordering[T] which can then be used for more typical Java/Scala-style comparisons.

Attributes

Returns

a new Ordering[T].

A method which maps this Comparer into a different Comparer, but of the same underlying type, according to the function f.

A method which maps this Comparer into a different Comparer, but of the same underlying type, according to the function f.

NOTE: formerly called map.

Value parameters

f

the function that takes a Comparison and yields a different Comparison.

Attributes

Returns

a new Comparer[T].

def tupled(t1: T, t2: T): Comparison

Method to apply this Comparer to two (tupled) parameters rather than the curried form. This is essentially the same as the compare method where the parameter is, explicitly, a (T,T).

Method to apply this Comparer to two (tupled) parameters rather than the curried form. This is essentially the same as the compare method where the parameter is, explicitly, a (T,T).

Value parameters

t1

the first T.

t2

the second T.

Attributes

Returns

the resulting Comparison.

Inherited methods

def andThen[A](g: T => Comparison => A): T => A

Composes two instances of Function1 in a new Function1, with this function applied first.

Composes two instances of Function1 in a new Function1, with this function applied first.

Type parameters

A

the result type of function g

Value parameters

g

a function R => A

Attributes

Returns

a new function f such that f(x) == g(apply(x))

Inherited from:
Function1
def compose[A](g: A => T): A => T => Comparison

Composes two instances of Function1 in a new Function1, with this function applied last.

Composes two instances of Function1 in a new Function1, with this function applied last.

Type parameters

A

the type to which function g can be applied

Value parameters

g

a function A => T1

Attributes

Returns

a new function f such that f(x) == apply(g(x))

Inherited from:
Function1
override def toString(): String

Returns a string representation of the object.

Returns a string representation of the object.

The default representation is platform dependent.

Attributes

Returns

a string representation of the object.

Definition Classes
Function1 -> Any
Inherited from:
Function1

Inherited and Abstract methods

def apply(v1: T): T => Comparison

Applies the body of this function to the argument.

Applies the body of this function to the argument.

Value parameters

v1

the function argument

Attributes

Returns

the result of function application.

Inherited from:
Function1