Help!
jmh530
john.michael.hall at gmail.com
Wed Jun 21 17:34:53 UTC 2023
On Wednesday, 21 June 2023 at 16:49:00 UTC, H. S. Teoh wrote:
> [snip]
> Hope this helps clear up the intended semantics of slide(). We
> should probably also document the above behaviour, because the
> current docs are not clear at all about exactly how this works.
>
>
> T
You can go pretty far with unittest examples.
```d
/// Example: windowSize=2, stepSize=1
unittest
{
import std.algorithm.comparison : equal;
import std.range: iota, slide;
auto x = 7.iota.slide(2, 1);
/**
Results in the following windows:
0 1 2 3 4 5 6
[ ]
[ ]
[ ]
[ ]
[ ]
[ ]
*/
assert(x.equal!equal(
[[0, 1],
[1, 2],
[2, 3],
[3, 4],
[4, 5],
[5, 6]]));
}
/// Example: windowSize=2, stepSize=2
unittest
{
import std.algorithm.comparison : equal;
import std.typecons: No;
import std.range: iota, slide;
auto x = 7.iota.slide(2, 2);
/**
Results in the following windows:
0 1 2 3 4 5 6
[ ]
[ ]
[ ]
[]
*/
assert(x.equal!equal(
[[0, 1],
[2, 3],
[4, 5],
[6]]));
// If prefer to drop the partial window, set `f` to
`No.withPartial`
auto y = 7.iota.slide!(No.withPartial)(2, 2);
/**
Results in the following windows:
0 1 2 3 4 5 6
[ ]
[ ]
[ ]
*/
assert(y.equal!equal(
[[0, 1],
[2, 3],
[4, 5]]));
}
/// Example: windowSize=2, stepSize=3
unittest
{
import std.algorithm.comparison : equal;
import std.range: iota, slide;
auto x = 9.iota.slide(2, 3);
/**
Results in the following windows:
0 1 2 3 4 5 6 7 8
[ ]
[ ]
[ ]
*/
assert(x.equal!equal(
[[0, 1],
[3, 4],
[6, 7]]));
}
```
More information about the Digitalmars-d
mailing list