Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala: How can I explicitly compare two Options?

If I have two Options such as

val a = Option(2)
val b = Option(1)

I can write

List(a,b).sorted

and it sorts correctly by inserting an implicit Ordering. How can I get a reference to this Ordering so I can call compare(a,b) and get the result? I'd like the equivalent of

val comparison = a.compare(b)

except without having a and b be instances of Ordered.

like image 573
Sam Avatar asked Apr 25 '14 03:04

Sam


2 Answers

You can just ask for the ordering implicit directly:

Welcome to Scala version 2.10.3 (Java HotSpot(TM) 64-Bit Server VM, Java 1.7.0_21).
Type in expressions to have them evaluated.
Type :help for more information.

scala> implicitly[Ordering[Option[Int]]]
res0: Ordering[Option[Int]] = scala.math.Ordering$$anon$3@501a9177

scala> res0.compare(Some(1), Some(3))
res1: Int = -1
like image 171
dlwh Avatar answered Oct 23 '22 09:10

dlwh


The best way to manipulate Options is using for expressions.

for (a1 <- a; b1 <- b) yield a1.compare(b1)  // Some(-1)

If at least one of the numbers is None, the result is None.

val x: Option[Int] = None
for (a1 <- a; b1 <- x) yield a1.compare(b1)  // None

A compare function may be defined as

def compare(a: Option[Int], b: Option[Int]) = {
  for (a1 <- a; b1 <- b) yield a1.compare(b1)
}.get

Updated:

If you want the Nones you can use pattern matching:

def compare(a: Option[Int], b: Option[Int]) = (a, b) match {
  case (Some(a), Some(b)) => a.compare(b)
  case (None, None)       => 0
  case (None, _)          => -1  // None comes before
  case (_, None)          => 1
}

val list: List[Option[Int]] = List(List(Some(1), None, Some(4), Some(2), None))
val list2 = list.sortWith(compare(_, _) < 0)    
//  list2 = List(None, None, Some(1), Some(2), Some(4))
like image 34
LRLucena Avatar answered Oct 23 '22 09:10

LRLucena