Skip to content

Instantly share code, notes, and snippets.

@milankinen
Last active December 26, 2015 00:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save milankinen/7062283 to your computer and use it in GitHub Desktop.
Save milankinen/7062283 to your computer and use it in GitHub Desktop.
Scala ordering DSL
object OrderingDSL {
case class AscendingOrder[T](value: T)(implicit ord: Ordering[T]) extends Ordered[AscendingOrder[T]] {
def compare(that: AscendingOrder[T]): Int = ord.compare(value, that.value)
}
case class DescendingOrder[T](value: T)(implicit ord: Ordering[T]) extends Ordered[DescendingOrder[T]] {
def compare(that: DescendingOrder[T]): Int = -ord.compare(value, that.value)
}
def asc[T](value: T)(implicit ord: Ordering[T]) = AscendingOrder(value)
def desc[T](value: T)(implicit ord: Ordering[T]) = DescendingOrder(value)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment