On Fri, Aug 6, 2010 at 19:41, bearophile <[email protected]> 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).
