score:1
If you define a semigroup instance for Int
, you can just add the validations together to get back one Task[ValidationNel[Throwable,Int]]
:
val sum2 = for {
three ← val3
four ← val4
} yield three +|+ four
score:2
You are probably looking for way to accumulate errors or compute result. You may be interested in macculkin operator, I mean a pipe at pipe or |@|
(choose the name you like most) ;)
Example of usage. See how errors are accumulated (OMG! and Holly mum! are included in result.
import scalaz._
import Scalaz._
val v1: ValidationNel[Throwable, Int] = 1.successNel
val v2: ValidationNel[Throwable, Int] = new RuntimeException("OMG!").failNel
val v3: ValidationNel[Throwable, Int] = new RuntimeException("Holly mom!").failNel
val sum : ValidationNel[Throwable, Int] = (v1 |@| v2 |@| v3 ) (_ + _ + _)
//scala> sum: scalaz.ValidationNel[Throwable,Int] = Failure(NonEmptyList(java.lang.RuntimeException: OMG!, java.lang.RuntimeException: Holly mom!))
The often made mistake is when you choose to use for comprehension or map functions. When you decide follow such way you will not able to acumulate errors. Only OMG! is accumulated. See below:
for( val1 <- v1;
val2 <- v2;
val3 <- v3
) yield(val1 + val2 + val3)
//res0: scalaz.Validation[scalaz.NonEmptyList[Throwable],Int] = Failure(NonEmptyList(java.lang.RuntimeException: OMG!))
Source: stackoverflow.com
Related Query
- Nice syntax for Validation in another monad
- Nice syntax for function composition in Scala
- scala extractor pattern for complex validation but with nice error output
- ScalaTest matcher syntax for checking whether one collection contains the elements of another
- Scalaz iteratees: "Lifting" `EnumeratorT` to match `IterateeT` for a "bigger" monad
- Reader Monad for Dependency Injection: multiple dependencies, nested calls
- What is the syntax for adding an element to a scala.collection.mutable.Map?
- Why is Scala's syntax for tuples so unusual?
- Is there a brief syntax for executing a block n times in Scala?
- Syntax sugar: _* for treating Seq as method parameters
- Method parameters validation in Scala, with for comprehension and monads
- Why does Scala not have a return/unit function defined for each monad (in contrast to Haskell)?
- Does Scala have syntax for 0- and 1-tuples?
- What is the Scala syntax for summing a List of objects?
- Using Reader Monad for Dependency Injection
- What is the accepted/recommended syntax for Scala code with lots of method-chaining?
- scala better syntax for map getOrElse
- Does Scala have record update syntax for making modified clones of immutable data structures?
- Is it possible for an optional argument value to depend on another argument in Scala
- What's the name of this Scala infix syntax for specifying type params?
- Monad transformer for NonEmptyList?
- What's the Scala syntax for a function taking any subtype of Ordered[A]?
- Looking for a nice way to split an array
- Clean Scala syntax for "Append optional value to Seq if it exists"
- Scala IDE - Play 2 Eclipse Plug-in not highlighting syntax for Scala HTML Templates
- What is the syntax for creating a Map in Scala that uses an enum as a key?
- Scala syntax for do-nothing function?
- Are Scala "continuations" just a funky syntax for defining and using Callback Functions?
- Is there syntax for adding a clue to ScalaTest matchers?
- Does Scala have syntax for projection of a nested singleton type?
More Query from same tag
- How can I loop through a Spark data frame
- How to sort a List of Lists of pairs of number in descending order by the second item in each pair in Scala?
- Marshaling akka-http
- Scala transform list of tuples to a tuple of lists
- Aggregate rows of Spark DataFrame to String after groupby
- Play Akka logger doesn't output debug messages to console
- More haskell-like applicative syntax in scalaz
- Type Cast: Map[String,String] to Map[String,Object]
- How can I publish an SBT project with build-time access to read-only private credentials?
- Spark-SQL using with
- Is there a way to include a Condition to adquire method in java (or scala) Semaphore object?
- Spark DataFrames when udf functions do not accept large enough input variables
- Deployment strategy for Jetty and Scala with SBT?
- How to sort a collection based on a custom list
- Insert query neither executed or neither gives exception
- Removing values after particular character from rdd in scala
- Cannot connect to remote MongoDB from EMR cluster with spark-shell
- Supplying "Class" type as a parameter to a method in Scala
- Scala remote actors on same machine
- select multiple edge property values from list of edges and put in list[List]
- What is the fastest way to transform a very large JSON file with Spark?
- Function in generic class
- What happens if SparkSession is not closed?
- Execute job on distributed cassandra DSE spark cluster
- Scala Typeclass for composites
- Chisel not finding the implicit value of a parameter
- sbt run a resource generator conditionally
- Spark RandomForest training StackOverflow error
- Spark DataFrames Scala - jump to next group during a loop
- Scala: arrays and type erasure