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

  • Home
  • SEARCH
  • 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 8705949
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T03:29:31+00:00 2026-06-13T03:29:31+00:00

I’d like to implement validation for a sequence of operations that all return Either[Error,Item]

  • 0

I’d like to implement validation for a sequence of operations that all return Either[Error,Item]
It should be fail-fast (in my initial need), I mean, returning Either[Error,Seq[Item]].
If there is an error, it’s obvious i do not want the following operations to be performed.
But in the future i may want to collect all the errors instead of returning only the first one.

I know Scalaz can do the job but for now I quite don’t understand all parts of Scalaz and I’m pretty sure there’s a simpler way to do it without using Scalaz, but using by-name parameters for exemple.

Is there a way to store by-name parameters in a sequence?
So that i can create a sequence of by-name values that represent my operations?

I mean, some kind of type Seq[=> Either[Error,Item]]
Then I could do something like calling takeWhile or collectFirst or something somilar, without all the operations being performed before the creation of the sequence?
I would expect the operations to be performed only when iterating on the sequence.

Thanks

  • 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-13T03:29:33+00:00Added an answer on June 13, 2026 at 3:29 am

    You can indeed use a Seq[() => Either[Error, Item]] to defer the computation at collection creation time. So for example

    val doSomething1: () => Either[Error, Item] = () => { println(1); Right(1) }
    val doSomething2: () => Either[Error, Item] = () => { println(2); Right(2) }
    val doSomething3: () => Either[Error, Item] = () => { println(3); Left("error") }
    val doSomething4: () => Either[Error, Item] = () => { println(4); Right(3) }
    val doSomething5: () => Either[Error, Item] = () => { println(5); Left("second error") }
    val l = Seq(doSomething1, doSomething2, doSomething3, doSomething4, doSomething5)
    

    (Items are Ints in the example and Errors are Strings)

    Then you can process them lazily stopping at first failure using the following recursive function:

    def processUntilFailure(l: Seq[() => Either[Error, Item]]): Either[Error, Seq[Item]] = {
      l.headOption.map(_.apply() match {
        case Left(error) => Left(error)
        case Right(item)  => processUntilFailure(l.tail).right.map(_ :+ item)
      }).getOrElse(Right(Nil))
    }
    

    So now when I run processUntilFailure(l)

    scala> processUntilFailure(l)
    1
    2
    3
    res1: Either[Error,Seq[Item]] = Left(error)
    

    If you wanted to generate a Either[Seq[String], Seq[Int]] (processing all the operations). You could do it with a little change:

    def processAll(l: Seq[() => Either[Error, Item]]): Either[Seq[Error], Seq[Item]] = {
      l.headOption.map(_.apply() match {
        case Left(error) => processAll(l.tail) match {
          case Right(_) => Left(Seq(error))
          case Left(previousErrors) => Left(previousErrors :+ error)
        }
        case Right(item)  => processAll(l.tail).right.map(_ :+ item)
      }).getOrElse(Right(Nil))
    }
    

    The only change as you can see is the Left case in the pattern match. Running this one:

    scala> processAll(l)
    1
    2
    3
    4
    5
    res0: Either[Seq[Error],Seq[Item]] = Left(List(second error, error))
    

    processAll can be replaced with a generic foldLeft on l

    val zero: Either[Seq[Error], Seq[Item]] = Right(Seq[Item]())
    l.foldLeft(zero) { (errorsOrItems: Either[Seq[Error], Seq[Item]], computation: () => Either[String, Int]) =>
      computation.apply().fold(
        { (error: String) => Left(errorsOrItems.left.toOption.map(_ :+ error).getOrElse(Seq(error))) },
        { (int: Int) => errorsOrItems.right.map(_ :+ int) })
    }
    

    processUntilFailure can as well but not easily. Since aborting early from a fold is tricky. Here’s a good answer about other possible approaches when you find yourself needing to do that.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a small JavaScript validation script that validates inputs based on Regex. I
For some reason, after submitting a string like this Jack’s Spindle from a text
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I would like to run a str_replace or preg_replace which looks for certain words
I am trying to render a haml file in a javascript response like so:
I have a French site that I want to parse, but am running into

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.