How to turn a List of Eithers to a Either of Lists using scalaz.MonadPlus.separate
Asked Answered
R

1

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 })
Ridenhour answered 27/4, 2016 at 0:59 Comment(3)
When in doubt, import it all.. import scalaz._, Scalaz._Behlke
If you want the specific imports you can look at my similar answer to what seems to be a duplicate of the linked question.Chartreuse
anyone knows how to do this in cats?Barbrabarbuda
B
1

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

Bamboo answered 27/4, 2016 at 7:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.