join

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Wed Jan 19 10:07:24 PST 2011


On 1/19/11 11:39 AM, Ary Manzana wrote:
> On 1/18/11 4:25 PM, Andrei Alexandrescu wrote:
>> I implemented a simple separatorless joiner as follows:
>>
>> auto joiner(RoR)(RoR r)
>
> Hi Andrei,
>
> What does it do? How do you use it?

Given a range of ranges, joiner concatenates them all with an optional 
separator. It's just like today's join(). The interesting part is that 
joiner never allocates memory or copy data - it operates directly on the 
original ranges. All it needs to do is keep track where it is. Moreover, 
if you decide to stop the iteration early, some work is saved.

import std.algorithm, std.stdio;
void main()
{
     auto stuff = [ "coat", "husky", "sled", ];
     writeln(joiner(stuff));
     writeln(joiner(stuff, "; "));
}

writes:

coathuskysled
coat; husky; sled

In brief you can in many places replace join() with joiner() and enjoy 
fewer memory allocations and less copies.

> And why "prime"? When I read it I remember the meaning of "prime
> number". I just looked up in the dictionary, it seems it also means
> "prepare". Why not use a simpler language and call it "prepare"?

Good idea, done. Will be part of the next commit. I plan to make one 
more pass through std.algorithm anyway. If there's stuff you wish were 
there (including stuff generalized from other modules), please let me know.


Andrei


More information about the Digitalmars-d mailing list