I was looking for an anwer to question Get next N elements from enumerable didn't find any satisfying and brewed my own. What I came up with was
IEnumerable<T> Chunk<T, R>(IEnumerable<R> src, int n, Func<IEnumerable<R>, T> action){
IEnumerable<R> head;
IEnumerable<R> tail = src;
while (tail.Any())
{
head = tail.Take(n);
tail = tail.Skip(n);
yield return action(head);
}
}
What I would really like though, is to have action have a default of t=>t
, but I can't figure out how to make that a default argument. The signature IEnumerable<T> Chunk<T, R>(IEnumerable<R> src, int n, Func<IEnumerable<R>, T> action = t=>t)
gives a syntax error.
My question is, how do I do this?
I suppose this is identical to Specifying a lambda function as default argument but for C# instead of C++
As a side note, I know it doesn't make any syntactical difference, but would it be easier to read if I switched T
and R
?