Maybe a stupid question but I just don't get it.
I have a Set<Either<Failure, Success>>
and want to output a Set<Success>
with Arrow-kt.
You can map the set like this for right:
val successes = originalSet.mapNotNull { it.orNull() }.toSet()
or if you want the lefts:
val failures = originalSet.mapNotNull { it.swap().orNull() }.toSet()
The final toSet()
is optional if you want to keep it as a Set
as mapNotNull
is an extension function on Iterable
and always returns a List
PS: No stupid questions :)
Update:
It can be done avoiding nullables
:
val successes = originalSet
.map { it.toOption() }
.filter { it is Some }
.toSet()
We could potentially add Iterable<Option<A>>.filterSome
and Iterable<Either<A, B>.mapAsOptions
functions.
Update 2:
That last example returns a Set<Option<Success>>
. If you want to unwrap the results without using null
then one thing you can try is to fold the Set
:
val successes = originalSet
.fold(emptySet<Success>()) { acc, item ->
item.fold({ acc }, { acc + it })
}
This last option (unintended pun) doesn't require the use of Option
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With