Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix issue 6621 - add sliding window iterator
- Loading branch information
Showing
2 changed files
with
1,020 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
`std.range.slides` (a fixed-size sliding window range) was added | ||
|
||
$(REF slides, std, range) allows to iterate a range in sliding windows: | ||
|
||
--- | ||
import std.array : array; | ||
import std.algorithm.comparison : equal; | ||
|
||
assert([0, 1, 2, 3].slides(2).equal!equal( | ||
[[0, 1], [1, 2], [2, 3]] | ||
)); | ||
assert(5.iota.slides(3).equal!equal( | ||
[[0, 1, 2], [1, 2, 3], [2, 3, 4]] | ||
)); | ||
|
||
assert(iota(7).slides(2, 2).equal!equal([[0, 1], [2, 3], [4, 5]])); | ||
assert(iota(12).slides(2, 4).equal!equal([[0, 1], [4, 5], [8, 9]])); | ||
|
||
// set a custom stepsize (default 1) | ||
assert(6.iota.slides(1, 2).equal!equal( | ||
[[0], [2], [4]] | ||
)); | ||
|
||
assert(6.iota.slides(2, 4).equal!equal( | ||
[[0, 1], [4, 5]] | ||
)); | ||
|
||
// allow slides with less elements than the window size | ||
assert(3.iota.slides!(No.slidesWithLessElements)(4).empty); | ||
assert(3.iota.slides!(Yes.slidesWithLessElements)(4).equal!equal( | ||
[[0, 1, 2]] | ||
)); | ||
--- |
Oops, something went wrong.