diff options
author | michelou <michelou@epfl.ch> | 2007-10-11 12:17:55 +0000 |
---|---|---|
committer | michelou <michelou@epfl.ch> | 2007-10-11 12:17:55 +0000 |
commit | ab477e33c3d389a34361e9474848da3d1e57a9fe (patch) | |
tree | 31fe93d42c843e96dd6e8a26ad5c9991c8e57e29 /src/library | |
parent | 57a7a38526cb853f2af78d3fe213bfe05a37ca44 (diff) | |
download | scala-ab477e33c3d389a34361e9474848da3d1e57a9fe.tar.gz scala-ab477e33c3d389a34361e9474848da3d1e57a9fe.tar.bz2 scala-ab477e33c3d389a34361e9474848da3d1e57a9fe.zip |
added method partition to Iterable (#171)
Diffstat (limited to 'src/library')
-rw-r--r-- | src/library/scala/Iterable.scala | 16 |
1 files changed, 16 insertions, 0 deletions
diff --git a/src/library/scala/Iterable.scala b/src/library/scala/Iterable.scala index 0acd9b824d..e00d41c7c8 100644 --- a/src/library/scala/Iterable.scala +++ b/src/library/scala/Iterable.scala @@ -187,6 +187,22 @@ trait Iterable[+A] { buf } + /** Partitions this iterable in two iterables according to a predicate. + * + * @param p the predicate on which to partition + * @return a pair of iterables: the iterable that satisfy the predicate + * <code>p</code> and the iterable that do not. + * The relative order of the elements in the resulting iterables + * is the same as in the original iterable. + */ + def partition[T](p: A => Boolean): (Iterable[A], Iterable[A]) = { + val matched = new ArrayBuffer[A] + val failed = new ArrayBuffer[A] + val elems = elements + while (elems.hasNext) { val x = elems.next; if (p(x)) matched += x else failed += x } + (matched, failed) + } + /** Returns the longest prefix of this iterable whose elements satisfy * the predicate <code>p</code>. * |