either

Validation versus disjunction

雨燕双飞 提交于 2019-11-27 11:43:17
Suppose I want to write a method with the following signature: def parse(input: List[(String, String)]): ValidationNel[Throwable, List[(Int, Int)]] For each pair of strings in the input, it needs to verify that both members can be parsed as integers and that the first is smaller than the second. It then needs to return the integers, accumulating any errors that turn up. First I'll define an error type: import scalaz._, Scalaz._ case class InvalidSizes(x: Int, y: Int) extends Exception( s"Error: $x is not smaller than $y!" ) Now I can implement my method as follows: def checkParses(p: (String,

Throwing exceptions in Scala, what is the “official rule”

蓝咒 提交于 2019-11-27 11:12:12
I'm following the Scala course on Coursera. I've started to read the Scala book of Odersky as well. What I often hear is that it's not a good idea to throw exceptions in functional languages, because it breaks the control flow and we usually return an Either with the Failure or Success. It seems also that Scala 2.10 will provide the Try which goes in that direction. But in the book and the course, Martin Odersky doesn't seem to say (at least for now) that exceptions are bad, and he uses them a lot. I also noticed the methods assert / require... Finally I'm a bit confused because I'd like to

Using Either to process failures in Scala code

左心房为你撑大大i 提交于 2019-11-26 21:30:30
Option monad is a great expressive way to deal with something-or-nothing things in Scala. But what if one needs to log a message when "nothing" occurs? According to the Scala API documentation, The Either type is often used as an alternative to scala.Option where Left represents failure (by convention) and Right is akin to Some. However, I had no luck to find best practices using Either or good real-world examples involving Either for processing failures. Finally I've come up with the following code for my own project: def logs: Array[String] = { def props: Option[Map[String, Any]] =

Best way to turn a Lists of Eithers into an Either of Lists?

99封情书 提交于 2019-11-26 19:42:50
问题 I have some code like the below, where I have a list of Eithers, and I want to turn it into an Either of Lists ... in particular (in this case), if there are any Lefts in the list, then I return a Left of the list of them, otherwise I return a Right of the list of the rights. val maybe: List[Either[String, Int]] = getMaybe val (strings, ints) = maybe.partition(_.isLeft) strings.map(_.left.get) match { case Nil => Right(ints.map(_.right.get)) case stringList => Left(stringList) } Calling get

Throwing exceptions in Scala, what is the “official rule”

99封情书 提交于 2019-11-26 15:31:42
问题 I'm following the Scala course on Coursera. I've started to read the Scala book of Odersky as well. What I often hear is that it's not a good idea to throw exceptions in functional languages, because it breaks the control flow and we usually return an Either with the Failure or Success. It seems also that Scala 2.10 will provide the Try which goes in that direction. But in the book and the course, Martin Odersky doesn't seem to say (at least for now) that exceptions are bad, and he uses them

Method parameters validation in Scala, with for comprehension and monads

六月ゝ 毕业季﹏ 提交于 2019-11-26 11:12:31
I'm trying to validate the parameters of a method for nullity but i don't find the solution... Can someone tell me how to do? I'm trying something like this: def buildNormalCategory(user: User, parent: Category, name: String, description: String): Either[Error,Category] = { val errors: Option[String] = for { _ <- Option(user).toRight("User is mandatory for a normal category").right _ <- Option(parent).toRight("Parent category is mandatory for a normal category").right _ <- Option(name).toRight("Name is mandatory for a normal category").right errors : Option[String] <- Option(description)

Using Either to process failures in Scala code

喜夏-厌秋 提交于 2019-11-26 07:59:16
问题 Option monad is a great expressive way to deal with something-or-nothing things in Scala. But what if one needs to log a message when \"nothing\" occurs? According to the Scala API documentation, The Either type is often used as an alternative to scala.Option where Left represents failure (by convention) and Right is akin to Some. However, I had no luck to find best practices using Either or good real-world examples involving Either for processing failures. Finally I\'ve come up with the

Method parameters validation in Scala, with for comprehension and monads

。_饼干妹妹 提交于 2019-11-26 02:18:38
问题 I\'m trying to validate the parameters of a method for nullity but i don\'t find the solution... Can someone tell me how to do? I\'m trying something like this: def buildNormalCategory(user: User, parent: Category, name: String, description: String): Either[Error,Category] = { val errors: Option[String] = for { _ <- Option(user).toRight(\"User is mandatory for a normal category\").right _ <- Option(parent).toRight(\"Parent category is mandatory for a normal category\").right _ <- Option(name)