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 9043137
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T10:40:29+00:00 2026-06-16T10:40:29+00:00

Could someone explain with real world examples how does work below methods of scalaz.Validation

  • 0

Could someone explain with real world examples how does work below methods of scalaz.Validation?
I mean loopSuccess and loopFailure.

Snippetes from source code (scalaz7):

scalaz.Validation:

/** Spin in tail-position on the success value of this validation. */
def loopSuccess[EE >: E, AA >: A, X](success: AA => X \/ Validation[EE, AA], failure: EE => X): X =
Validation.loopSuccess(this, success, failure)

/** Spin in tail-position on the failure value of this validation. */
def loopFailure[EE >: E, AA >: A, X](success: AA => X, failure: EE => X \/ Validation[EE, AA]): X =
Validation.loopFailure(this, success, failure)

Companion object:

object Validation extends ValidationFunctions with ValidationInstances {

  /** Spin in tail-position on the success value of the given validation. */
  @annotation.tailrec
  final def loopSuccess[E, A, X](d: Validation[E, A], success: A => X \/ Validation[E, A], failure: E => X): X =
    d match {
      case Failure(e) => failure(e)
      case Success(a) => success(a) match {
        case -\/(x) => x
        case \/-(q) => loopSuccess(q, success, failure)
      }
    }

  /** Spin in tail-position on the failure value of the given validation. */
  @annotation.tailrec
  final def loopFailure[E, A, X](d: Validation[E, A], success: A => X, failure: E => X \/ Validation[E, A]): X =
    d match {
      case Failure(e) => failure(e) match {
        case -\/(x) => x
        case \/-(q) => loopFailure(q, success, failure)
      }
      case Success(a) => success(a)
    }

}
  • 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-16T10:40:31+00:00Added an answer on June 16, 2026 at 10:40 am

    This is similar to a trampoline. for loopSuccess, you provide an initial value, and a function which takes you to the next state. There are 3 possible next states:

    X.left           // stop processing with X as the result
    Success(a).right // no result, run the next iteration with this value
    Failure(e).right // stop processing, run the failure function on this result and return it
    

    For loopFailure does the same thing with Failure and Success reversed, so you keep running until you return a left or a Success.

    Here is a example of using loopSuccess:

    import scalaz._
    import Scalaz._
    
    object TestLoopSuccess extends App {
      // check if a number divides another, returning a Failure for division by zero
      val divides : Int => Int => Validation[String,Boolean] = { div => num =>
        if(div == 0) "division by zero".failure
        else (num % div == 0).success
      }
    
      val allDivide : Int => List[Int] => String \/ Validation[Int,List[Int]] = { div => nums => 
        nums match {
          // empty list means we are done, so we return a left
          case Nil => "All numbers divide".left
    
          // process the head of the list and return a right
          case x::xs => divides(div)(x).flatMap { divides => 
            if(divides) 
              // head divides, so process more of the list
              xs.success 
            else 
              // head does not divide, so we are done
              "%d is not a multiple of %d".format(x,div).failure
          }.right
        }
      }
    
      println(Validation.loopSuccess(List(2,4,6,8).success[String], allDivide(0), identity[String])) // "division by zero"
      println(Validation.loopSuccess(List(2,4,6,8).success[String], allDivide(2), identity[String])) // "All numbers divide"
      println(Validation.loopSuccess(List(2,4,7,8).success[String], allDivide(2), identity[String])) // "7 is not a multiple of 2"
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Could someone explain and provide real world examples of using with keyword in defining
Could someone explain in layman terms what this actually does? XmlTextReader textReader = new
Please could someone explain to me why this regex does not match anything, when
Could someone explain why the below slicing works? >>> S='SPAM' >>> S[1:1] ''
could someone explain me what XOR , (^) does in the fallowing code exaclly
Could someone explain me different situations to use AlarmManager vs Handler with examples please.
Could someone explain me what the following snippet of assembly code does? I'm not
Could someone explain the difference between the Copy Bundle Resources phase of Xcode and
Could someone explain what I am doing wrong with my classes that my JTabbedPane
Could someone explain to me how can I run my py2exe program, a console

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.