I’m finding myself doing a bunch of validations on...
# arrow
s
I’m finding myself doing a bunch of validations on an unknown number of items and I end up with a
List<ValidatedNel<E, A>>
and I’m converting that to a
ValidatedNel<E, List<A>>
. Is there a more idiomatic way of doing that? The actual flow of data is more like this:
List<A> -> List<ValidatedNel<E, B>> -> ValidatedNel<E, List<B>>
. It works, but it feels like there is probably something more “native” to arrow that would handle this kind of flow.
a
that’s traverse! (sorry, classic FP joke 😄)
😂 2
😄 1
🤣 1
but yes, traverse should do what you need, also sequence if you don’t need to map your items
s
awesome, I knew there had to be a particular operation that corresponded to the “shape” of that flow
I’ll take a look at that
s
Yes, you want.
Copy code
val aas: List<A> = listOf(...)

val validated: List<ValidatedNel<E, B>> = aas.map(::validate)

val res: ValidatedNel<E, List<B>> = validated.sequence(Validated.applicative(NonEmptyList.applicative()).map { it.fix() }
OR
Copy code
val aas: List<A> = listOf(...)

aas.traverse(Validated.applicative(NonEmptyList.applicative()) { a ->
  validate(a)
}.map { it.fix() }
🔝 4
s
Traverse did the trick and now I get to delete a bunch of code! Thanks everyone
👏 3