I was playing around with ListW.<^>
, the definition of which is as follows:
def <^>[B: Zero](f: NonEmptyList[A] => B): B = value match {
case Nil => ∅
case h :: t => f(Scalaz.nel(h, t))
}
I cannot figure out how come Option
is being chosen as the Zero
type for this example
scala> case class CC(v : Int)
defined class CC
scala> val posns = List(CC(2), CC(5), CC(1))
posns: List[CC] = List(CC(2), CC(5), CC(1))
So now I have a List of these things. My goal is to get back an Option[CC]
for the min/max of posns
where I get a None
for min if there are no values below zero and similar for max.
scala> import scalaz._; import Scalaz._
import scalaz._
import Scalaz._
scala> implicit val CCOrder = new Order[CC] {
| def order(v1 : CC, v2 : CC) = orderBy( (v : CC) => v.v ).order(v1, v2)
| }
CCOrder: java.lang.Object with scalaz.Order[CC] = $anon$1@1e48d65
scala> posns.filter(_.v < 0) <^> (_.min)
res0: Option[CC] = None
scala> posns.filter(_.v > 0) <^> (_.max)
res1: Option[CC] = Some(CC(5))
Option was exactly the Zero
type I wanted! Can anyone explain how come Option
is being chosen by the typer? I don't declare it anywhere!