Skip to content

Instantly share code, notes, and snippets.

@arturaz
Created November 18, 2018 20:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arturaz/521140b0cd744340655f1264645eb47d to your computer and use it in GitHub Desktop.
Save arturaz/521140b0cd744340655f1264645eb47d to your computer and use it in GitHub Desktop.
either sequence
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