import scalaz._, Scalaz._
type ErrorMessage = String
def parse(value: String): ErrorMessage \/ Double =
value.parseDouble match {
case Success(x: Double) => \/.right[ErrorMessage, Double](x)
case _ => \/.left(s"Could not parse '$value'")
}
def divide(d: Double): ErrorMessage \/ Double =
if(d == 0.0) \/.left("Cannot divide by 0.0.")
else \/.right(100 / d)
scala> parse("2.0").flatMap(divide)
res0: scalaz.\/[ErrorMessage,Double] = \/-(50.0)
scala> parse("2.0") >>= divide
res1: scalaz.Unapply[scalaz.Bind,scalaz.\/[ErrorMessage,Double]]{type M[X] = scalaz.\/[ErrorMessage,X]; type A = Double}#M[Double] = \/-(50.0)
Hi,
About Unapply, that should help:
http://typelevel.org/blog/2013/09/11/using-scalaz-Unapply.html
http://eed3si9n.com/learning-scalaz/Unapply.html
Cheers
--
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.
For more options, visit https://groups.google.com/d/optout.