You could use sequenceU
to transform List[Throwable \/ String]
to Throwable \/ List[String]
(it will preserve only first Throwable
) and than you should just use flatMap
like this:
def source: Throwable \/ List[Throwable \/ String] = ???
def result: Throwable \/ List[String] = source.flatMap{_.sequenceU}
You could also use traverseU
instead of map
+ sequenceU
:
def readlines: Throwable \/ List[String] = ???
def parseLine[A](s: String): Throwable \/ A = ???
def parseLines[A](): Throwable \/ List[A] =
readlines flatMap { _ traverseU parseLine[A] }
Using for-comprehension:
def parseLines[A](): Throwable \/ List[A] =
for {
l <- readlines
r <- l traverseU parseLine[A]
} yield r