Skip to content

Instantly share code, notes, and snippets.

@arturaz
Last active December 24, 2015 13:49
Show Gist options
  • Save arturaz/6808298 to your computer and use it in GitHub Desktop.
Save arturaz/6808298 to your computer and use it in GitHub Desktop.
def extractEither[
Key, TLeft, TRight, M[_] <: TraversableOnce[_]
]
(monad: M[(Key, Either[TLeft, TRight])])
(implicit cbf: CanBuildFrom[
M[(Key, Either[TLeft, TRight])],
(Key, TRight),
M[(Key, TRight)]
]): Either[TLeft, M[(Key, TRight)]] = {
val builder = cbf(monad)
builder.sizeHint(monad.size)
monad.foreach { x =>
// ^___ missing parameter type - why?
val (key, either) = x.asInstanceOf[(Key, Either[TLeft, TRight])]
either.fold(
leftVal => return Left(leftVal),
rightVal => builder += ((key, rightVal))
)
}
Right(builder.result())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment