Scala 2.13 Collection: how to collect T in Seq[Either[_,T]]

I generate list of Either[A,B] and use partition to obtain 2 collections of A and B so:

      val (bad, good) = l.partition(_.isLeft)
      val b = bad.map(_.left.getOrElse(Error("Error.check failed getting error")))
      val g = good.map(_.right.get)

but both right and get are deprecated as shown in the ScalaDocs below:

right: RightProjection[A, B]
Projects this Either as a Right.

Because Either is right-biased, this method is not normally needed.

So how am I supposed to extract the types within?

TIA.

Instead of either.right.something and either.left.something you’re supposed to do either.something and either.swap.something.

As to your original problem, it seems there’s function specifically for your use case in Scala 2.13. It’s Seq.partitionMap:

val input = Seq(Left(3), Right("a"), Left(0), Right("bbb"))
val (lefts, rights) = input.partitionMap(identity)
println(lefts)
println(rights)
1 Like

@tarsa thank you.