9
有人可以用現實世界的例子來解釋如何在scalaz.Validation
的方法下工作嗎?我的意思是loopSuccess
和loopFailure
。從源代碼(scalaz7)工作方式scalaz.Validation循環成功與循環失敗
Snippetes:
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)
播對象:
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)
}
}