Skip to content

Commit

Permalink
Merge pull request #4027 from wilzbach/pairwise
Browse files Browse the repository at this point in the history
slides: a sliding window range iterator
merged-on-behalf-of: unknown
  • Loading branch information
dlang-bot authored and andralex committed May 16, 2017
2 parents c33c670 + f92adce commit be63fc7
Show file tree
Hide file tree
Showing 2 changed files with 1,031 additions and 3 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 be63fc7

Please sign in to comment.