diff options
author | Paul Phillips <paulp@improving.org> | 2010-01-16 06:37:37 +0000 |
---|---|---|
committer | Paul Phillips <paulp@improving.org> | 2010-01-16 06:37:37 +0000 |
commit | d9b01e2c583dd4a99f3861bb41db50cd9f448700 (patch) | |
tree | 6fa4d7cd42a0c76ad426fdc3a2ec6b169ad3a5a0 /src/library | |
parent | 36ef60e68c03bc1c7fd2e910ae7d70d4ec32d3bf (diff) | |
download | scala-d9b01e2c583dd4a99f3861bb41db50cd9f448700.tar.gz scala-d9b01e2c583dd4a99f3861bb41db50cd9f448700.tar.bz2 scala-d9b01e2c583dd4a99f3861bb41db50cd9f448700.zip |
Made Iterator consistent with Iterable by addin...
Made Iterator consistent with Iterable by adding grouped and sliding to
IterableLike. Closes #2837. Review by community.
Diffstat (limited to 'src/library')
-rw-r--r-- | src/library/scala/collection/IterableLike.scala | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/src/library/scala/collection/IterableLike.scala b/src/library/scala/collection/IterableLike.scala index 2e86b90479..8446988821 100644 --- a/src/library/scala/collection/IterableLike.scala +++ b/src/library/scala/collection/IterableLike.scala @@ -138,6 +138,38 @@ self => b.result } + /** Partitions elements in fixed size ${coll}s. + * @see Iterator#grouped + * + * @param size the number of elements per group + * @return An iterator producing ${coll}s of size `size`, except the + * last will be truncated if the elements don't divide evenly. + */ + def grouped(size: Int): Iterator[Repr] = + for (xs <- iterator grouped size) yield { + val b = newBuilder + b ++= xs + b.result + } + + /** Groups elements in fixed size blocks by passing a "sliding window" + * over them (as opposed to partitioning them, as is done in grouped.) + * @see Iterator#sliding + * + * @param size the number of elements per group + * @param step the distance between the first elements of successive + * groups (defaults to 1) + * @return An iterator producing ${coll}s of size `size`, except the + * last will be truncated if the elements don't divide evenly. + */ + def sliding[B >: A](size: Int): Iterator[Repr] = sliding(size, 1) + def sliding[B >: A](size: Int, step: Int): Iterator[Repr] = + for (xs <- iterator.sliding(size, step)) yield { + val b = newBuilder + b ++= xs + b.result + } + /** Selects last ''n'' elements. * $orderDependent * |