Davide Giuseppe Farella
03/21/2023, 5:22 PMValidated<A, B>
, if they were Either
I would do, for example
either {
val a = eitherA.bind()
val b = eitherB.bind()
Combine(a, b)
}
What’s the right way to do it with Validated
? Either with the either
block or any other solution 🙂phldavies
03/21/2023, 5:35 PMzip
extension for this but you'd need to provide some way of merging A
in case both were invalid. Usually this is covered by using ValidatedNel<A, B>
(a typealias for Validated<NonEmptyList<A>, B>
)Davide Giuseppe Farella
03/21/2023, 5:37 PMValidated<String, Int>
? 🙂Either
, so I’m struggling to understand how to use itphldavies
03/21/2023, 5:38 PMDavide Giuseppe Farella
03/21/2023, 5:38 PMValidateddNel
, I just need an example of how to use zip
, then I’ll take it from there 🙂phldavies
03/21/2023, 5:42 PMValidated
is going to be deprecated, and removed from Arrow 2.0.0 to simplify this.Davide Giuseppe Farella
03/21/2023, 5:44 PMphldavies
03/21/2023, 5:44 PMValidatedNel
it's relatively simple to use .validNel()
and .invalidNel()
to construct your values much like you would .valid()
and .invalid()
. Once you have two values you can join them using a.zip(b) { validA, validB -> doSomethingWith(validA, validB) }
zipOrAccumulate
set of functions that will provide the same logic (combining one or more lefts, or providing all rights to a function)Davide Giuseppe Farella
03/21/2023, 5:46 PMValidatedNel
I felt less stupid and everything worked straight away 🙂phldavies
03/21/2023, 6:01 PMValidated<A, B>
you need to provide an additional argument to zip of a Semigroup<A>
(which is effectively just a function A.(A) -> A
like String::plus
)
With a custom combine to comma-delimit errors you can have
lateinit var a: Validated<String, Int>
lateinit var b: Validated<String, Int>
fun String.combine(other: String) = "$this, $other"
a.zip(String::combine, b) { validA, validB -> validA + validB }
Davide Giuseppe Farella
03/21/2023, 6:10 PM