6

如何用 scalaz 实现这样的行为:

"Fail1".failNel[Int] and "Fail2".failNel[Int] to Failure("Fail1", "Fail2")
"Fail1".failNel[Int] and 100.successNel[String] to Success(100)

我的解决方案看起来很复杂,我想存在其他一些简洁的方法:

  def aggregateErrorsOrSuccess(v1: ValidationNEL[String, Int], 
                               v2: ValidationNEL[String, Int]) = {
    v2.fold(
      nl => (nl.fail[Int] |@| v1) {(i1, i2) => (/*actually should never happen*/)},
      res => res.successNel[String]
    )
  }

======================

我的第二个解决方案:

implicit def nel2list[T](nl: NonEmptyList[T]) = nl.head :: nl.tail;

implicit def ValidationNELPlus[X]: Plus[({type λ[α]=ValidationNEL[X, α]})#λ] = new      Plus[({type λ[α]=ValidationNEL[X, α]})#λ] {
def plus[A](a1: ValidationNEL[X, A], a2: => ValidationNEL[X, A]) = a1 match {
    case Success(_) => a1
    case Failure(f1) => a2 match {
      case Success(_) => a2
      case Failure(f2) => (f1 <::: f2).fail[A]
    }
  }
}

像这样使用它:

val sum = v1 <+> v2
4

1 回答 1

6

实际上,您可以使用与您的第二个解决方案接近的>>*<<定义的(紧急出口?)方法。Validation但是,它也会尝试汇总成功,您可能需要对其进行调整。

def >>*<<[EE >: E: Semigroup, AA >: A: Semigroup](x: Validation[EE, AA]): Validation[EE, AA] = (this, x) match {
  case (Success(a1), Success(a2)) => Success((a1: AA) ⊹ a2)
  case (Success(a1), Failure(_)) => Success(a1)
  case (Failure(_), Success(a2)) => Success(a2)
  case (Failure(e1), Failure(e2)) => Failure((e1: EE) ⊹ e2)
}
于 2012-02-08T22:15:33.823 回答