Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

163
Views
How to typesafe reduce a Collection of Either to only Right

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.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

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.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!