range chunks

Philippe Sigaud philippe.sigaud at gmail.com
Fri Aug 6 12:02:21 PDT 2010


On Fri, Aug 6, 2010 at 19:41, bearophile <bearophileHUGS at lycos.com> wrote:

> Philippe Sigaud:
> > 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.
>
> It's better to give it a default chunk size of 2.
>

Why?
And what should the default step be, according to you? If chose n (the chunk
size), because that's want the OP wanted.


If I have understood this well, then this is the partition() function:
> http://reference.wolfram.com/mathematica/ref/Partition.html
>

Yeah, partition, chunk, segment, it a basic thing, worth including in
std.range.
Very useful in conjunction with mapping: calculating the moving average, for
example.


>
> If you want to be more complete you can add two more features: to start
> counting items from the end (but they are given from the start) and give a
> "duplication window", so for example the natural numbers with a window of 2
> and a duplication of 1 are [1, 2], [2, 3], [3, 4].


This simple code does this already: chunks(range, 2, 1).
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d/attachments/20100806/be6fd552/attachment.html>


More information about the Digitalmars-d mailing list