range chunks
Philippe Sigaud
philippe.sigaud at gmail.com
Fri Aug 6 10:33:09 PDT 2010
2010/8/6 Adrian Matoga <epi at atari8.info>
> Hi,
>
> Is there any off the shelf solution for iterating over a range by chunks?
None that I know of.
(should substitute [1, 2, 3], [4, 5, 6], [7, 8, 9], [10] for chunk in
> subsequent iterations)
>
>
As a data point, why do you think it should produce [10] and not stop at
[7,8,9]?
Here is what I cooked, it's still a bit rough around the edges. It has an
optional step argument, to see how many elements to jump.
import std.range;
struct Chunks(R) if (isInputRange!R)
{
R range;
size_t n; // chunk size
size_t step; // how many elements to jump
bool empty() @property { return range.empty;}
ElementType!R[] front() @property { return array(take(range, n));} //
inefficient if you call front() many times in a row
void popFront() @property { popFrontN(range, step);}
static if (hasLength!R)
size_t length() @property { return (range.length+step-1)/step;}
}
Chunks!R chunks(R)(R range, size_t n) if (isInputRange!R)
{
return Chunks!R(range, n, n); // default is step == n
}
Chunks!R chunks(R)(R range, size_t n, size_t step) if (isInputRange!R)
{
return Chunks!R(range, n, step);
}
Philippe
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d/attachments/20100806/8f24bf1b/attachment.html>
More information about the Digitalmars-d
mailing list