2015-11-18 90 views
1

假設我需要累積功能失敗。我可以scalaz輕鬆地做到這一點:如何積累scala.util.Try失敗?

val foo: String => Throwable\/Int = {s => \/.fromTryCatchNonFatal(s.toInt)} 
val v1 = foo("abc").validation.toValidationNel 
val v2 = foo("xyz").validation.toValidationNel 
val v = (v1 |@| v2) {_ + _} 
val e = v.toDisjunciton 

現在我有一個函數,該函數返回的scala.util.Try代替scalaz.\/

val f: String => Try[Int] = {s => Try(s.toInt)} 

什麼是積累scala.util.Try失敗最好的辦法?我是否需要手動將scala.util.Try轉換爲scalaz.Validation,反之亦然?

回答

2

也許這可以幫助你:

def accumulate[T](tries:Try[T]*) = tries.collect {case Failure(t) => t}