我有一个输入值列表
List[A]
和一个函数
f(List[A]): Either[Failure, Success]
我将该函数应用于列表的每个元素,结果是
List[Either[Failure, Success]]
.我想检查列表,如果有任何值失败,则返回第一个失败,否则返回成功列表。
我使用了以下模式:
val allValues = list.map(f(_))
if (allValues.exists(_.isLeft)) {
allValues.find(_.isLeft).get
} else {
allValues.collect {
case Right(result) => result
}
}
和
val allValues = list.map(f(_))
val failures = allValues.collect { case Left(error) => error }
if (failures.nonEmpty) {
failures(0)
} else {
allValues.collect {
case Right(result) => result
}
}
有没有更简洁的方式来表达这种模式?
有时,我必须通过另一个函数进一步处理成功,再次使用相同的模式。例如
最佳答案
听起来好像您想转换 List[Either[Failure, Success]]
成Either[Failure, List[Success]]
?您可以使用 toLeft
使这更优雅.
result collectFirst { case Left(f) => f } toLeft {
result collect { case Right(r) => r}
}
collectFirst
接受 PartialFunction[A, B]
它将应用于 List
的第一个元素具有定义的输出,并将返回 Option[B]
.在这种情况下,我尝试提取第一个 Left(f)
来自 List
,所以我会得到 Option[Failure]
.然后,我调用
toLeft
在 Option[Failure]
.这将转换单个 Failure
再次到Left
如果Option
包含一个值,参数将产生 Right
如果 Option
的值是空的。如果
Option
确实是空的,然后我使用 collect
提取成功,类似于使用 collectFirst
,除了它保留了 List
的所有元素PartialFunction
被定义为。
关于scala - 过滤 Scala 失败的任一列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28090819/