Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8747795
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T12:25:00+00:00 2026-06-13T12:25:00+00:00

I need to reduce an Iterable[Either[Throwable, String]] to an Either[Throwable, Iterable[String]]. I don’t know

  • 0

I need to reduce an Iterable[Either[Throwable, String]] to an Either[Throwable, Iterable[String]]. I don’t know if this operation is pretty common or not, haven’t found nothing on the Iterable trait. So I have written this function:

def reduce[A, B](xs: Iterable[Either[A, B]]): Either[A, Iterable[B]] = 
  xs.collectFirst {
    case Left(x) => x
  } match {
    case Some(x) => Left(x)
    case None => Right(xs.collect{case Right(y)=> y})
  }

Can anyone help me to find a better way if this one it isn’t?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T12:25:01+00:00Added an answer on June 13, 2026 at 12:25 pm

    This operation is often called sequencing, and is available in the standard libraries of some functional languages (such as Haskell). In Scala you can either implement your own, or use an external library like Scalaz. Suppose we have the following, for example:

    val xs: List[Either[String, Int]] = List(Right(1), Right(2))
    val ys: List[Either[String, Int]] = List(Right(1), Left("1st!"), Left("2nd!"))
    

    Now we can write (using Scalaz 7):

    scala> import scalaz._, Scalaz._
    import scalaz._
    import Scalaz._
    
    scala> xs.sequenceU
    res0: Either[String,List[Int]] = Right(List(1, 2))
    
    scala> ys.sequenceU
    res1: Either[String,List[Int]] = Left(1st!)
    

    As desired.


    As a side note, this operation just requires that the outside container be traversable and that the inside container be an applicative functor. Scalaz also provides a ValidationNEL class that’s a lot like Either and also fits these requirements, but using sequence on a list of ValidationNELs collects multiple errors instead of stopping at the first:

    val zs: List[ValidationNEL[String, Int]] =
      List(1.successNel, "1st".failNel, "2nd".failNel)
    

    Now we get:

    scala> print(zs.sequenceU)
    Failure(NonEmptyList(1st, 2nd))
    

    You could also use sequence on a list of Options, Promises, etc.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to reduce this boolean expression to its simplest form. Its given that
Need a map reduce function by mongo in php This my mongo structure [_id]
I need to compress a string to reduce the size of a web service
If I don't do any map/reduce jobs, still JobTracker/TaskTrackers need to be running for
I need to reduce the font size of the table view header. My string
Is there a way to reduce jQuery to functions that I need. In this
I need to reduce the size of apk file which currently is 40M. This
I need to reduce that space, ... How can I do it? Planificación, prioridad
I will build a system where I want to reduce single-point-of-failures, and I need
Need to apply a filter to a file like this: TUPAC_0006:1:1:2554:2356#0/1 0 * 0

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.