给定foo
和bar
:
scala> def foo: Try[Seq[String]] = Success(List("good", "job"))
foo: scala.util.Try[Seq[String]]
scala> def bar(x: String): Try[String] = Try(x)
bar: (x: String)scala.util.Try[String]
我怎样才能把它转换flatMap
成一个for-comprehension
?
scala> foo.flatMap(a => Try(a.map(c => bar(c))))
res48: scala.util.Try[Seq[scala.util.Try[String]]] =
Success(List(Success(good), Success(job)))
请注意,我提供了以上内容作为Using for-comprehension, Try and sequences in Scala的答案,但我无法将其转换为for-comphension
.