Created
November 18, 2018 20:20
-
-
Save arturaz/521140b0cd744340655f1264645eb47d to your computer and use it in GitHub Desktop.
either sequence
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package app.utils | |
import scala.collection.generic.CanBuildFrom | |
import scala.language.higherKinds | |
object EitherUtils { | |
def sequence[ | |
OuterTraversable[X1] <: TraversableOnce[X1], | |
InnerTraversable[X2] <: TraversableOnce[X2], | |
A, B | |
]( | |
eithers: OuterTraversable[Either[A, InnerTraversable[B]]] | |
)(implicit cbf: CanBuildFrom[Nothing, B, InnerTraversable[B]]): Either[A, InnerTraversable[B]] = { | |
val builder = cbf() | |
eithers.foreach { | |
case Left(a) => return Left(a) | |
case Right(cc2) => cc2.foreach { builder += _ } | |
} | |
Right(builder.result()) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment