fold on empty range

Mitacha mateusz.mitaszka at gmail.com
Wed Feb 17 10:15:10 UTC 2021


On Wednesday, 17 February 2021 at 09:21:47 UTC, Rumbu wrote:
> In the expression below:
>
> return matchAll(content, keywordsPattern)
>             .map!(a => a.hit.stripLeft("[").strip("]"))
>             .fold!((a, b) => a ~ "," ~ b)
>             .splitter(',')
>             .map!(a => a.stripLeft("\" ").strip("\" "))
>             .filter!(a => !a.any!(b => b == ' ' || b == '\\' || 
> b == '/' || b == ':'))
>             .array
>             .sort
>             .uniq;
>
>
> fold is throwing an exception if the result of the previous map 
> is empty. Is there any way to express something to convince 
> fold to return the empty range received from map?
>
> Of course, I know I can test for empty in a separate 
> expression, but I'd like to keep my expression flow as it is.

I think you can try using `fold` with seed value:
```
              .map!(a => a.hit.stripLeft("[").strip("]"))
              .fold!((a, b) => a ~ "," ~ b)("")
              .splitter(',')
```
it'll use empty string as first element in range.

BTW perheps you could use `joinner` instead of this `fold` to 
join values with ",".


More information about the Digitalmars-d-learn mailing list