following is a toy example to demonstrate real life legacy methods' shape weirdness and point of the question.
As you can see anotherFunc
, after mapping over personList
expands type to \/[Throwable,List[\/[Throwable,String]]]
which isn't intended return type but effect of map
ing personList
. again what's illustrated below inside anotherFunc
is for demo purpose(in reality there is more meaningful things happening instead of Option("fakeString")
or any of that.
Requirement is to map
personList
and if it's right
then do something with each element of the List[Person]
returned from right
(side that was returned from the disjunction).
how to simplify/flatten return type of anotherFunc
(to return \/[Throwable,List[String]]
). May be using other combinators?
case class Person(name :String)
def personList : \/[Throwable,List[Person]] ={
\/.fromTryCatch{
List(Person("John"))
}
}
def anotherFunc : \/[Throwable,List[\/[Throwable,String]]]= {
personList.map{ pl =>
pl.map{p =>
for{
s <- Option("fakeString").\/>(new Throwable("not found"))
} yield s
}
}
}
traverse
was all I was adding I would have made it a comment. – Ibbie