0

How to turn a List of Eithers to a Either of Lists, using MonadPlus.separate?

In this answer the author claims this solution, but fails to provide the imports or complete example:

If scalaz is one of your dependencies I would simply use separate:

val el : List[Either[Int, String]] = List(Left(1), Right("Success"), Left(42))

scala> val (lefts, rights) = el.separate
lefts: List[Int] = List(1, 42)
rights: List[String] = List(Success)

Is this a real working solution? I see that MonadPlus has a separate function but I still didn't manage to make it work.

ps: I am aware I can achieve this without scalaz, such as the example below. However, in this question I am asking how to use scalaz.MonadPlus.separate to achieve this.

(lefts, rights) = (el.collect { case Left(left) => left }, el.collect { case Right(right) => right })
Community
  • 1
  • 1
David Portabella
  • 12,390
  • 27
  • 101
  • 182

1 Answers1

1

That solution is correct, you're just missing import scalaz.Scalaz._ here.

Paweł Jurczenko
  • 4,431
  • 2
  • 20
  • 24