fold on empty range

Mitacha mateusz.mitaszka at gmail.com
Wed Feb 17 12:58:29 UTC 2021


On Wednesday, 17 February 2021 at 11:38:45 UTC, Rumbu wrote:
> On Wednesday, 17 February 2021 at 10:15:10 UTC, Mitacha wrote:
>
>> it'll use empty string as first element in range.
>>
>> BTW perheps you could use `joinner` instead of this `fold` to 
>> join values with ",".
>
> Thanks for that. I thought to joiner too, but it doesn't work. 
> I need fold to take a list of strings and concatenate them. 
> Basically I read comma separated keywords from various sources 
> and i want to iterate through all of them. If you know other 
> method without the involved allocation of fold...
>
> .map!(a => a.hit.stripLeft("[").strip("]"))     //"k1,k2", 
> "k3,k4" ...
> .fold!((a, b) => a ~ "," ~ b)("")               
> //"k1,k2,k3,k4,..."
> .splitter(',')                                  //"k1", "k2", 
> "k3", "k4", ...,
> .map!(a => a.stripLeft("\" '").strip("\" '"))
> .filter!(a => a.length && !a.any!(b => b == ' ' || b == '\\' || 
> b == '/' || b == ':'))
> .array
> .sort
> .uniq;

If you replace `fold` and `splitter` with this, then it doesn't 
allocate:
```
auto fn() @nogc {
     return only("k1,k2", "k3,k4")
         .map!(x => x.splitter(","))
         .joiner;
}

void main() {
    auto range = fn();
    range.writeln;
}
```


More information about the Digitalmars-d-learn mailing list