@@ -477,26 +477,43 @@ trait IterableOps[+A, +CC[_], +C] extends Any with IterableOnce[A] with Iterable
477
477
478
478
/** Groups elements in fixed size blocks by passing a "sliding window"
479
479
* 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.
481
485
* @see [[scala.collection.Iterator ]], method `sliding`
482
486
*
483
487
* @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))`
487
496
*/
488
497
def sliding (size : Int ): Iterator [C ] = sliding(size, 1 )
489
498
490
499
/** Groups elements in fixed size blocks by passing a "sliding window"
491
500
* 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
+ *
492
507
* @see [[scala.collection.Iterator ]], method `sliding`
493
508
*
494
509
* @param size the number of elements per group
495
510
* @param step the distance between the first elements of successive
496
511
* 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
499
514
* 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))`
500
517
*/
501
518
def sliding (size : Int , step : Int ): Iterator [C ] =
502
519
iterator.sliding(size, step).map(fromSpecific)
0 commit comments