Skip to content

Instantly share code, notes, and snippets.

@Allan-Gong
Created May 30, 2018 23:39
Show Gist options
  • Save Allan-Gong/c4565fb4358fb31b44ec2680d557402f to your computer and use it in GitHub Desktop.
Save Allan-Gong/c4565fb4358fb31b44ec2680d557402f to your computer and use it in GitHub Desktop.
// sliding[B >: A](size: Int, step: Int = 1): GroupedIterator[B]
// Returns an iterator which presents a "sliding window" view of another iterator.
// The first argument is the window size, and the second is how far to advance the window on each iteration; defaults to 1.
// Example usages:
// Returns List(List(1, 2, 3), List(2, 3, 4), List(3, 4, 5))
(1 to 5).iterator.sliding(3).toList
// Returns List(List(1, 2, 3, 4), List(4, 5))
(1 to 5).iterator.sliding(4, 3).toList
// Returns List(List(1, 2, 3, 4))
(1 to 5).iterator.sliding(4, 3).withPartial(false).toList
// Returns List(List(1, 2, 3, 4), List(4, 5, 20, 25))
// Illustrating that withPadding's argument is by-name.
val it2 = Iterator.iterate(20)(_ + 5)
(1 to 5).iterator.sliding(4, 3).withPadding(it2.next).toList
// Note:
// Reuse: After calling this method, one should discard the iterator it was called on, and use only the iterator that was returned.
// Using the old iterator is undefined, subject to change, and may result in changes to the new iterator as well.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment