Relation between scala Ordering and Scalaz Equals and Order

140 views
Skip to first unread message

Edmondo Porcu

unread,
May 21, 2013, 11:59:42 AM5/21/13
to sca...@googlegroups.com
Dear all,

I am confused about these three typeclasses.

Which one should I prefer between Order and Ordering? Is there an implicit conversion from one to the other, and if it is the case, where is it found?

Best Regards
Edmondo

Tom Switzer

unread,
May 21, 2013, 12:05:51 PM5/21/13
to sca...@googlegroups.com
Ordering is an ADT that consists of 3 cases: LT, GT, and EQ. Order is a type class that defines an Order for some type. Scalaz's Order#compare returns an Ordering. You typically use both.


--
You received this message because you are subscribed to the Google Groups "scalaz" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scalaz+un...@googlegroups.com.
To post to this group, send email to sca...@googlegroups.com.
Visit this group at http://groups.google.com/group/scalaz?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Edmondo Porcu

unread,
May 21, 2013, 12:24:16 PM5/21/13
to sca...@googlegroups.com
What about implicit conversion? Which one do you implement?


2013/5/21 Tom Switzer <thomas....@gmail.com>

Tom Switzer

unread,
May 21, 2013, 12:29:34 PM5/21/13
to sca...@googlegroups.com
You implement scalaz.Order[A]. scalaz.Ordering is a sealed trait with 3 case objects as sub-types. You cannot implement it. It looks something like:

sealed trait Ordering
object Ordering {
  case object LT extends Ordering
  case object GT extends Ordering
  case object EQ extends Ordering
}

Instead of returning an Int from Order#compare, scalaz returns a scalaz.Ordering.

Edmondo Porcu

unread,
May 21, 2013, 12:37:47 PM5/21/13
to sca...@googlegroups.com
And when I want to do a sort on a collection, what should I import? Thank you for the precious indications


2013/5/21 Tom Switzer <thomas....@gmail.com>

Tom Switzer

unread,
May 21, 2013, 12:48:15 PM5/21/13
to sca...@googlegroups.com
Scalaz's Order has a method toScalaOrdering that returns a scala.math.Ordering instance. You can put this in scope as an implicit:

implicit val o = Order[A].toScalaOrdering

and then use Scala's collection's ordering methods.
Reply all
Reply to author
Forward
0 new messages