diff options
Diffstat (limited to 'src/library')
-rw-r--r-- | src/library/scala/RandomAccessSeq.scala | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/src/library/scala/RandomAccessSeq.scala b/src/library/scala/RandomAccessSeq.scala index 66faeb10e1..437d7ae8fb 100644 --- a/src/library/scala/RandomAccessSeq.scala +++ b/src/library/scala/RandomAccessSeq.scala @@ -10,6 +10,8 @@ package scala +import collection.mutable.ArrayBuffer + object RandomAccessSeq { trait Projection[+A] extends Seq.Projection[A] with RandomAccessSeq[A] { override def projection = this @@ -161,6 +163,24 @@ trait RandomAccessSeq[+A] extends Seq[A] { override def reverse : RandomAccessSeq.Projection[A] = RandomAccessSeq.this.projection } + /** Partitions this random access sequence in two random access + * sequences according to a predicate. + * + * @param p the predicate on which to partition + * @return a pair of random access sequences: the sequence of all elements + * that satisfy the predicate <code>p</code> and the sequence of + * all elements that do not. + * The relative order of the elements in the resulting sequences + * is the same as in the original sequence. + */ + override def partition(p: A => Boolean): (RandomAccessSeq[A], RandomAccessSeq[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) + } + /** insert segment <code>patch</code> into this sequence at <code>from</code> * replacing <code>replaced</code> elements. The result is a projection. */ |