CLOVIS
08/15/2020, 1:16 PMValidated.applicative<KnownError>().map(vId, vName, vAge, { (id, name, age) ->
Person(id, name, age)
}).ev()
Would it be fair to say that Applicative is ‘just' a generalization of flatMap
that allows to call it on multiple objects?
Maybe something like
vId.flatMap { id ->
vName.flatMap { name ->
vAge.flatMap { age ->
Person(id, name, age)
}
}
}
Or is that completely wrong?simon.vergauwen
08/15/2020, 1:25 PMflatMap
is used to sequence dependent operations. In this case the next function cannot execute if the previous failed.
Whilst in your original example that is not the case.
i.e.
getUserById(id).flatMap { user ->
fetchInfoForUser(user).flatMap { info ->
process(info)
}
}
This is also the reason why you will not find flatMap
on. Validated
since then the behaviour would be identical to Either
.simon.vergauwen
08/15/2020, 1:26 PMmapN
offers a way to combine indepent operations. Which means you can run them independently of each-other, and in the case of ValidatedNel
combine the results.
But for the case of Either
, that logic is implemented by flatMap
since there we want to short-circuit on errors.simon.vergauwen
08/15/2020, 1:30 PMEither
it simply delegates to flatMap
but that's not the case for all implementors of Applicative
.CLOVIS
08/15/2020, 1:58 PMsimon.vergauwen
08/15/2020, 3:47 PMValidated
doesn't even have flatMap
simon.vergauwen
08/15/2020, 3:53 PMEither
map
is implemented over flatMap
, and in the case of Validated
you can see that it's implemented over bimap
.
So Validated
doesn't short-circuit like Either
does, which allows the accumulations of errors.simon.vergauwen
08/15/2020, 3:55 PMSemigroup<E>
for Validated.applicative
and you don't have to do so for Either
.
Since Validated
can continue on errors, and Either
can't.simon.vergauwen
08/15/2020, 3:55 PMCLOVIS
08/16/2020, 9:08 AM