Skip to content

Commit e9c5346

Browse files
author
Martijn Hoekstra
committed
expand on sliding doc
be more specific on the more specific overload include examples
1 parent 23dea65 commit e9c5346

File tree

2 files changed

+28
-8
lines changed

2 files changed

+28
-8
lines changed

library/src/scala/collection/Iterable.scala

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -477,26 +477,43 @@ trait IterableOps[+A, +CC[_], +C] extends Any with IterableOnce[A] with Iterable
477477

478478
/** Groups elements in fixed size blocks by passing a "sliding window"
479479
* over them (as opposed to partitioning them, as is done in `grouped`.)
480-
* The "sliding window" step is set to one.
480+
*
481+
* An empty collection returns an empty iterator, and a non-empty
482+
* collection containing fewer elements than the window size returns
483+
* an iterator that will produce the original collection as its only
484+
* element.
481485
* @see [[scala.collection.Iterator]], method `sliding`
482486
*
483487
* @param size the number of elements per group
484-
* @return An iterator producing ${coll}s of size `size`, except the
485-
* last element (which may be the only element) will be truncated
486-
* if there are fewer than `size` elements remaining to be grouped.
488+
* @return An iterator producing ${coll}s of size `size`, except for a
489+
* non-empty collection with less than `size` elements, which
490+
* returns an iterator that produces the source collection itself
491+
* as its only element.
492+
* @example `List().sliding(2) = empty iterator`
493+
* @example `List(1).sliding(2) = Iterator(List(1))`
494+
* @example `List(1, 2).sliding(2) = Iterator(List(1, 2))`
495+
* @example `List(1, 2, 3).sliding(2) = Iterator(List(1, 2), List(2, 3))`
487496
*/
488497
def sliding(size: Int): Iterator[C] = sliding(size, 1)
489498

490499
/** Groups elements in fixed size blocks by passing a "sliding window"
491500
* over them (as opposed to partitioning them, as is done in grouped.)
501+
*
502+
* The returned iterator will be empty when called on an empty collection.
503+
* The last element the iterator produces may be smaller than the window
504+
* size when the original collection isn't exhausted by the window before
505+
* it and its last element isn't skipped by the step before it.
506+
*
492507
* @see [[scala.collection.Iterator]], method `sliding`
493508
*
494509
* @param size the number of elements per group
495510
* @param step the distance between the first elements of successive
496511
* groups
497-
* @return An iterator producing ${coll}s of size `size`, except the
498-
* last element (which may be the only element) will be truncated
512+
* @return An iterator producing ${coll}s of size `size`, except the last
513+
* element (which may be the only element) will be smaller
499514
* if there are fewer than `size` elements remaining to be grouped.
515+
* @example `List(1, 2, 3, 4, 5).sliding(2, 2) = Iterator(List(1, 2), List(3, 4), List(5))`
516+
* @example `List(1, 2, 3, 4, 5, 6).sliding(2, 3) = Iterator(List(1, 2), List(4, 5))`
500517
*/
501518
def sliding(size: Int, step: Int): Iterator[C] =
502519
iterator.sliding(size, step).map(fromSpecific)

library/src/scala/collection/Iterator.scala

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ trait Iterator[+A] extends IterableOnce[A] with IterableOnceOps[A, Iterator, Ite
335335
* the second argument `step` is how far to advance the window
336336
* on each iteration. The `step` defaults to `1`.
337337
*
338-
* The default `GroupedIterator` can be configured to either
338+
* The returned `GroupedIterator` can be configured to either
339339
* pad a partial result to size `size` or suppress the partial
340340
* result entirely.
341341
*
@@ -353,7 +353,10 @@ trait Iterator[+A] extends IterableOnce[A] with IterableOnceOps[A, Iterator, Ite
353353
* (1 to 5).iterator.sliding(4, 3).withPadding(it2.next).toList
354354
* }}}
355355
*
356-
* @return An iterator producing `Seq[B]`s of size `size`, except the
356+
* @param size the number of elements per group
357+
* @param step the distance between the first elements of successive
358+
* groups
359+
* @return A `GroupedIterator` producing `Seq[B]`s of size `size`, except the
357360
* last element (which may be the only element) will be truncated
358361
* if there are fewer than `size` elements remaining to be grouped.
359362
* This behavior can be configured.

0 commit comments

Comments
 (0)