digitalmars.D.learn - Slide - what does withPartial do?
- Piotr Mitana (9/9) Mar 01 2018 For some reason this is true:
- Simen =?UTF-8?B?S2rDpnLDpXM=?= (6/15) Mar 01 2018 It seems it should only do anything when the window size is
- Seb (53/65) Mar 01 2018 No it's not a bug.
For some reason this is true: slide!(Yes.withPartial)([1, 2, 3, 4, 5], 3).array == [[1, 2, 3], [2, 3, 4], [3, 4, 5]] Shouldn't it rather return [[1], [1, 2], [1, 2, 3], [2, 3, 4], [3, 4, 5], [4, 5], [5]], or at least [[1, 2, 3], [2, 3, 4], [3, 4, 5], [4, 5], [5]]? I can see no difference on the result when withPartial is on and off. Is it a bug or I don't understand it correctly?
Mar 01 2018
On Thursday, 1 March 2018 at 08:31:05 UTC, Piotr Mitana wrote:For some reason this is true: slide!(Yes.withPartial)([1, 2, 3, 4, 5], 3).array == [[1, 2, 3], [2, 3, 4], [3, 4, 5]] Shouldn't it rather return [[1], [1, 2], [1, 2, 3], [2, 3, 4], [3, 4, 5], [4, 5], [5]], or at least [[1, 2, 3], [2, 3, 4], [3, 4, 5], [4, 5], [5]]? I can see no difference on the result when withPartial is on and off. Is it a bug or I don't understand it correctly?It seems it should only do anything when the window size is larger than the range being iterated over. So something like slide!(Yes.withPartial)([0,1,2], 4).array = [[0,1,2]]. -- Simen
Mar 01 2018
On Thursday, 1 March 2018 at 08:31:05 UTC, Piotr Mitana wrote:For some reason this is true: slide!(Yes.withPartial)([1, 2, 3, 4, 5], 3).array == [[1, 2, 3], [2, 3, 4], [3, 4, 5]] Shouldn't it rather return [[1], [1, 2], [1, 2, 3], [2, 3, 4], [3, 4, 5], [4, 5], [5]], or at least [[1, 2, 3], [2, 3, 4], [3, 4, 5], [4, 5], [5]]? I can see no difference on the result when withPartial is on and off. Is it a bug or I don't understand it correctly?No it's not a bug. Yes.withPartial (the default) means that if the last element in the range doesn't have the full size it still gets printed, with No.withPartial it's not part of the range. It's gets clearer with a different step size: --- foreach (i; 5 .. 10) slide!(Yes.withPartial)(i.iota, 3, 4).writeln; --- --- [[0, 1, 2], [4]] [[0, 1, 2], [4, 5]] [[0, 1, 2], [4, 5, 6]] [[0, 1, 2], [4, 5, 6]] [[0, 1, 2], [4, 5, 6], [8]] --- --- foreach (i; 5 .. 10) slide!(No.withPartial)(i.iota, 3, 4).writeln; --- --- [[0, 1, 2]] [[0, 1, 2]] [[0, 1, 2], [4, 5, 6]] [[0, 1, 2], [4, 5, 6]] [[0, 1, 2], [4, 5, 6]] --- https://run.dlang.io/is/x8Q5x8 In fact for the default step size of 1, this only has effect if the windowSize is larger than the range size. BTW as a recent addition to std.range, it comes with a ton of unittests: https://github.com/dlang/phobos/blob/master/std/range/package.d#L7755 (though I agree that the public documentation could explain No.withPartial better.)That wouldn't be a sliding window / rolling window operator. The idea of a sliding / rolling window operator is that all windows have the same size. withPartial just allows you to pick the behavior for how the final element. That's also how other languages implement this. For examples, here's how sliding from Scala's standard library behaves: --- (1 to 5).iterator.sliding(6).withPartial(false).toList // List() (1 to 5).iterator.sliding(6).withPartial(true).toList // List(List(1, 2, 3, 4, 5)) (1 to 5).iterator.sliding(3, 4).withPartial(false).toList // List(List(1, 2, 3)) (1 to 5).iterator.sliding(3, 4).withPartial(true).toList // List(List(1, 2, 3), List(5)) --- https://scastie.scala-lang.org/pR5pH6DRTWuiVR7GNUTbJAShouldn't it rather return [[1], [1, 2], [1, 2, 3], [2, 3, 4],[3, 4, 5], [4, 5], [5]], or at least [[1, 2, 3], [2, 3, 4], [3, 4, 5], [4, 5], [5]]?
Mar 01 2018