What's the best way to combine errors in List<V...
# arrow
p
What's the best way to combine errors in List<ValidatedNel<MyError, Unit>> to ValidatedNel<MyError, Unit> ?
a
Copy code
val SE = Nel.semigroup<MyError>()
val ap = ValidatedNel.applicative(SE)
val validated = list.sequence(ap).fix()
?
s
Wouldn't that result in a
ValidatedNel<MyError, List<Unit>>
?
I guess
List<Unit>
is probably as useful as a
Unit
though.
Otherwise I think you should be able to do
Copy code
list.reduce { a, b ->
    a.combineK(Nel.semigroup(), b)
}
Although I think
combineK
won't collect validation errors, if either of them are successful.
I suspect you'd need to define a semigroup instance for
Unit
and use
combine
rather than
combineK
.
Like:
Copy code
object MonoidUnit : Monoid<Unit> {
    override fun Unit.combine(b: Unit) = this
    override fun empty() = Unit
}

list.reduce { a, b ->
    a.combine(Nel.semigroup(), MonoidUnit, b)
}
a
Wouldn’t that result in a 
ValidatedNel<MyError, List<Unit>>
?
It will, yes. If you need a
Unit
result you could go with the Unit monoid or just add a
.map { Unit }
☝️ 1
p
Thank you!