Ben Flowers Ben Flowers - 1 month ago 13
Scala Question

Cats Seq[Xor[A,B]] => Xor[A, Seq[B]]

I have a sequence of Errors or Views (


I want to map this to an Xor of the first error (if any) or a Sequence of Views
Xor[Error, Seq[View]]
) or possibly simply (

How can I do this?


You can use sequenceU provided by the bitraverse syntax, similar to as you would do with scalaz. It doesn't seem like the proper type classes exist for Seq though, but you can use List.

import cats._, data._, implicits._, syntax.bitraverse._

case class Error(msg: String)

case class View(content: String)

val errors: List[Xor[Error, View]] = List(
  Xor.Right(View("abc")), Xor.Left(Error("error!")), 

val successes: List[Xor[Error, View]] = List(

scala> errors.sequenceU
res1:[Error,List[View]] = Left(Error(error!))

scala> successes.sequenceU
res2:[Error,List[View]] = Right(List(View(abc), View(xyz)))