for-comprehension

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)

withFilter instead of filter

╄→гoц情女王★ 提交于 2019-11-26 08:13:42
问题 Is it always more performant to use withFilter instead of filter, when afterwards applying functions like map, flatmap etc.? Why are only map, flatmap and foreach supported? (Expected functions like forall/exists as well) 回答1: From the Scala docs: Note: the difference between c filter p and c withFilter p is that the former creates a new collection, whereas the latter only restricts the domain of subsequent map , flatMap , foreach , and withFilter operations. So filter will take the original

Scala “<-” for comprehension

萝らか妹 提交于 2019-11-26 05:22:46
问题 I have found that Scala always has a \"natural explanation\" to anything. Always something like \"ohh, but that\'s just a function being called on this and that object with this and that parameter\". In a sense, nothing is really compiler-magic as we know it from other languages. My question is on the <- operator as used in the following code: for(i <- 0 to 10) println(i) In this example I can see it being rewritten to something like: 0.to(10).foreach((i:Int)=>println(i)) but this doesn\'t

Type Mismatch on Scala For Comprehension

元气小坏坏 提交于 2019-11-26 03:08:38
问题 Why does this construction cause a Type Mismatch error in Scala? for (first <- Some(1); second <- List(1,2,3)) yield (first,second) <console>:6: error: type mismatch; found : List[(Int, Int)] required: Option[?] for (first <- Some(1); second <- List(1,2,3)) yield (first,second) If I switch the Some with the List it compiles fine: for (first <- List(1,2,3); second <- Some(1)) yield (first,second) res41: List[(Int, Int)] = List((1,1), (2,1), (3,1)) This also works fine: for (first <- Some(1);

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)