summaryrefslogtreecommitdiff
path: root/main/core/src/util/EitherOps.scala
diff options
context:
space:
mode:
Diffstat (limited to 'main/core/src/util/EitherOps.scala')
-rw-r--r--main/core/src/util/EitherOps.scala18
1 files changed, 18 insertions, 0 deletions
diff --git a/main/core/src/util/EitherOps.scala b/main/core/src/util/EitherOps.scala
new file mode 100644
index 00000000..da2552c8
--- /dev/null
+++ b/main/core/src/util/EitherOps.scala
@@ -0,0 +1,18 @@
+package mill.util
+
+import scala.collection.generic.CanBuildFrom
+import scala.collection.mutable
+import scala.language.higherKinds
+
+object EitherOps {
+
+ // implementation similar to scala.concurrent.Future#sequence
+ def sequence[A, B, M[X] <: TraversableOnce[X]](in: M[Either[A, B]])(
+ implicit cbf: CanBuildFrom[M[Either[A, B]], B, M[B]]): Either[A, M[B]] = {
+ in.foldLeft[Either[A, mutable.Builder[B, M[B]]]](Right(cbf(in))) {
+ case (acc, el) =>
+ for (a <- acc; e <- el) yield a += e
+ }
+ .map(_.result())
+ }
+}