Does Scalaza have something that can be copied both with an error and with success?

I started using Scalaz 7 Validation and / or disjunction to process a list of possible failures and manage their outcome.

There are two well-documented cases for such use cases:

1 / You want to check the list of conditions for something and accumulate each error, if any. Here you always go to the end of the list, and in case of any error you have a failure as a global result. And this applied functor at work.

2 / You want to perform several steps that may fail, and stop the first failure. Here we have a monad that is well suited for Scala to understand.

So, I have two other use cases that apply to the same lines, but don't seem good anyway: I want to process the list of steps, possibly fail, and accumulate both errors and success results (for example, this a list of changes to files, errors can occur, because the outside world and success is a patch that I want to save for later).

The difference in the two use cases is only that I want to stop earlier (by the first error) or go to the end of the list.

Ok, so what is needed for this?

(asking a question makes me think it's just a simple foldLeft, right? I'll give a question here to check, and if anyone else asks a question)

+5
source share
4 answers

I would do something like this:

scala> List(1.success[String], 2.success[String], "3".failure[Int], "4".failure[Int]).partition(_.isSuccess)
res2: (List[scalaz.Validation[java.lang.String,Int]], List[scalaz.Validation[java.lang.String,Int]]) = (List(Success(1), Success(2)),List(Failure(3), Failure(4)))

scala> val fun = (_:List[Validation[String, Int]]).reduceLeft(_ append _)
fun: List[scalaz.Validation[String,Int]] => scalaz.Validation[String,Int] = <function1>

scala> fun <-: res2 :-> fun
res3: (scalaz.Validation[String,Int], scalaz.Validation[String,Int]) = (Success(3),Failure(34))

UPD: # 129 # 130, fun (_:List[Validation[String, Int]]).concatenate (_:List[Validation[String, Int]]).suml

bimap:

scala> List(1.success[String], 2.success[String], "3".failure[Int], "4".failure[Int]).partition(_.isSuccess).bimap(_.suml, _.suml)
res6: (scalaz.Validation[java.lang.String,Int], scalaz.Validation[java.lang.String,Int]) = (Success(3),Failure(34))
+4

Validation#append Validation#+|+. , , . , . . Semigroup .

+5

Either[E, A] Writer[List[E], A]. Writer , .

+1

, (SomveValue, List[T]), T "", "" "", , .

, Scalaz - .

0
source

All Articles