Skip to content

Commit

Permalink
Fix issue 6621 - add sliding window iterator
Browse files Browse the repository at this point in the history
  • Loading branch information
wilzbach committed Feb 17, 2017
1 parent 3024c32 commit db2bf8f
Show file tree
Hide file tree
Showing 2 changed files with 1,020 additions and 0 deletions.
33 changes: 33 additions & 0 deletions changelog/std-range-slides.dd
@@ -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]]
));
---

0 comments on commit db2bf8f

Please sign in to comment.