Hi Stefan, the signatures fit, basically. Thanks! It's not exactly what I want, though: due to performance issues I must have some computations to come first and only if all of those succeed I want to continue with the others.
Regarding your proposal `(iToA(i) flatMap aToC) |@| (iToB(i) flatMap bToD) apply calcO`, I first must check `iToA` and `iToB` which are very fast checks. If one of those fails then "stop". If both succeed then go on with the more expensive computations, e.g. `aToC`.
So I came up with the following:
type I = String; type O; type A = String; type B = String; type C; type D
type ValRes[+A] = ValidationNel[String,A]
def iToA(i: I): ValRes[A] = "a".success // "iToA failed".failNel
def aToC(a: A): ValRes[C] = "aToC failed".failNel
def iToB(i: I): ValRes[B] = "b".success // "iToB failed".failNel
def bToD(b: B): ValRes[D] = "bToD failed".failNel
def calcO(c: C, d: D): O = ???
val f: I => ValRes[O] = i =>
((iToA(i) |@| iToB(i)).tupled.flatMap { case (a,b) =>
(aToC(a) |@| bToD(b)) apply calcO
}).fold(
(es: NonEmptyList[String]) => {
logger.error(s"""Invalid params:\n${es.list mkString "\n"}""")
es.failure
},
(o: O) => {
o.success
})
Is `tupled` the right direction here?
I guess actually my question is the following: how to control nested validations and accumulate all errors along the way? For example, I have a couple of validations v1, ..., vn which I want to check at the very beginning. Then I have another couple of validations w1, ..., wm which I want to check if v1, ..., vn succeeded. Then I have another couple of validations ... . If `tupled` is the right direction, than the pattern could be:
((v1 |@| v2 |@| ... |@| vn).tupled.flatMap { case (v1, ..., vn) =>
((w1 |@| w2 |@| ... |@| wm)).tupled.flatMap { case (w1, ..., wm) =>
// if all validations succeed
doFinalComputation(): Result
}
}).fold(
(allError: NonEmptyList[String]) => {
logger.error(s"""Invalid params:\n${allError.list mkString "\n"}""")
allError.failure
},
(r: Result) => {
r.success
})
What do you think? Cheers, /nm
On Tuesday, May 7, 2013 1:14:12 PM UTC+2, Stefan Hoeck wrote: