Skip to content

Instantly share code, notes, and snippets.

@jsuereth
Created August 27, 2010 02:38
Show Gist options
  • Save jsuereth/552675 to your computer and use it in GitHub Desktop.
Save jsuereth/552675 to your computer and use it in GitHub Desktop.
Index: src/library/scala/collection/TraversableLike.scala
===================================================================
--- src/library/scala/collection/TraversableLike.scala (revision 22850)
+++ src/library/scala/collection/TraversableLike.scala (working copy)
@@ -313,6 +313,19 @@
for (x <- this) (if (p(x)) l else r) += x
(l.result, r.result)
}
+ /** Partitions this $coll in two ${colls} according to the
+ * transformation to an Either.
+ *
+ * @param p function that generates an either.
+ * @return a pair of ${coll}s: The first $coll consists of all elements
+ * mapped to the Left of the either. The relative order of the elements
+ * in the resulting ${coll}s is the same as the orginal $coll
+ */
+ def partitionEither[B,C,To,To2](p : A => Either[B,C])(implicit cbf1 : CanBuildFrom[Repr, B, To], cbf2 : CanBuildFrom[Repr, C, To2]) : (To, To2) = {
+ val (left, right) = (cbf1(), cbf2())
+ foreach(p(_).fold(left +=, right +=))
+ (left.result(),right.result())
+ }
/** Partitions this $coll into a map of ${coll}s according to some discriminator function.
*
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment