range chunks

KennyTM~ kennytm at gmail.com
Sat Aug 7 03:35:59 PDT 2010


On Aug 7, 10 07:52, bearophile wrote:
> Philippe Sigaud:
>>> It's better to give it a default chunk size of 2.
>>
>> Why?
>
> I'm using partition() for years in various languages, and I've seen that the chunks of size 2 are the most common.
>
>
>> And what should the default step be, according to you? If chose n (the chunk
>> size), because that's want the OP wanted.
>
> No duplication across chunks, so on default it can split the natural numbers as:
> [1, 2], [3, 4], [5, 6], etc.
>
>
>> Yeah, partition, chunk, segment, it a basic thing, worth including in
>> std.range.
>
> The most common name is partition().
>

In D, 'partition' is already used for rearranging (splitting) a range 
into two according to a predicate*. (It's the same in C++, Haskell and 
Ruby.) Since the name is already taken, it's very bad to break the 
compatibility to change what the function does even if the name were 
more common.

Also, in Python this function called grouper() (in itertool's 
"Recipes"), and in Ruby it's called each_slice. I've never seen it 
called Partition[] besides Mathematica.

*: http://www.digitalmars.com/d/2.0/phobos/std_algorithm.html#partition

> Bye,
> bearophile



More information about the Digitalmars-d mailing list