6
어떻게 가능 scalaz 같은 행동을 구현하는 것입니다 :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