Normalizing chains of .Skip() and .Take() calls
Posted
by dtb
on Stack Overflow
See other posts from Stack Overflow
or by dtb
Published on 2010-03-26T13:01:09Z
Indexed on
2010/03/26
13:03 UTC
Read the original article
Hit count: 416
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?
© Stack Overflow or respective owner