scala - Transform and flatten List of disjunctions -
case class errs(errors: list[err]) case class err(exceptionmessage: string, custommessage: string, statuscode: int, extrainfo: option[string] = none)
one of functions returning val result = list[\/[errs, boolean]]
in order transform/sequence , flatten result
following passes done
val finalres: \/[errs, boolean] = result.map(_.swap).sequenceu.map(x => errs(x.map(_.errors).flatten)).swap
this looks inelegant , performance intensive (given number of passes has via multiple map
calls etc)
is there combinator in scalaz
can make more elegant less number of passes or demands writing custom function?
firstly, use traverseu
, suggested travis brown.
after that, call map
followed call flatten
can written flatmap
.
val finalres: \/[errs, boolean] = result.traverseu(_.swap).map(x => errs(x.flatmap(_.errors))).swap
moreover, may consider using type alias errs
.
type errs = list[err]
so, code simpler (less boxing/unboxing) :
val finalres: \/[errs, boolean] = result.traverseu(_.swap).map(_.flatten).swap