Recommendations on avoiding range pipeline type hell

Mike Parker aldacron at gmail.com
Sat May 15 13:43:29 UTC 2021


On Saturday, 15 May 2021 at 11:25:10 UTC, Chris Piker wrote:

> Is there some obvious trick or way of looking at the problem 
> that I'm missing?
>

In addition to what Adam said, if you do need to store the result 
for use in a friendlier form, just import `std.array` and append 
`.array` to the end of the pipeline. This will eagerly allocate 
space for and copy the range elements to an array, i.e., convert 
the range to a container:

```d
auto mega_range = range1.range2!(lambda2).range3!(lambda3).array;
```

Sometimes you may want to set up a range and save it for later 
consumption, but not necessarily as a container. In that case, 
just store the range itself as you already do, and pass it to a 
consumer when you're ready. That might be `.array` or it could be 
`foreach` or something else.


```d
auto mega_range = range1.range2!(lambda2).range3!(lambda3);

// later
foreach(elem; mega_range) {
    doStuff(elem);
}
```


More information about the Digitalmars-d-learn mailing list