I'm trying to normalize arbitrary chains of .Skip()
and .Take()
calls to a single .Skip()
call followed by an optional single .Take()
call.
Here are some examples of expected results, but I'm not sure if these are correct:
.Skip(5) => .Skip(5)
.Take(7) => .Skip(0).Take(7)
.Skip(5).Skip(7) => .Skip(12)
.Skip(5).Take(7) => .Skip(5).Take(7)
.Take(7).Skip(5) => .Skip(5).Take(2)
.Take(5).Take(7) => .Skip(0).Take(5)
.Skip(5).Skip(7).Skip(11) => .Skip(23)
.Skip(5).Skip(7).Take(11) => .Skip(12).Take(11)
.Skip(5).Take(7).Skip(3) => .Skip(8).Take(4)
.Skip(5).Take(7).Take(3) => .Skip(5).Take(4)
.Take(11).Skip(5).Skip(3) => .Skip(8).Take(3)
.Take(11).Skip(5).Take(7) => .Skip(5).Take(6)
.Take(11).Take(5).Skip(3) => .Skip(3).Take(2)
.Take(11).Take(5).Take(3) => .Skip(0).Take(3)
Can anyone confirm these are the correct results to be expected?
Here is the basic algorithm that I derived from the examples:
class Foo
{
private int skip;
private int? take;
public Foo Skip(int value)
{
if (value < 0)
value = 0;
this.skip += value;
if (this.take.HasValue)
this.take -= value;
return this;
}
public Foo Take(int value)
{
if (value < 0)
value = 0;
if (!this.take.HasValue || value < this.take)
this.take = value;
return this;
}
}
Any idea how I can confirm if this is the correct algorithm?