Window
Enumerates arrays ("windows") with the length of size
containing the most current values.
{ 0, 1, 2, 3 }
becomes to { [0, 1], [1, 2], [2, 3] }
.
I am using this for example to draw a line graph by connecting two points.
public static IEnumerable Window(
this IEnumerable source)
{
return source.Window(2);
}
public static IEnumerable Window(
this IEnumerable source, int size)
{
if (size <= 0)
throw new ArgumentOutOfRangeException("size");
return source.Skip(size).WindowHelper(size, source.Take(size));
}
private static IEnumerable WindowHelper(
this IEnumerable source, int size, IEnumerable init)
{
Queue q = new Queue(init);
yield return q.ToArray();
foreach (var value in source)
{
q.Dequeue();
q.Enqueue(value);
yield return q.ToArray();
}
}