is std.algorithm.joiner lazy?

Edwin van Leeuwen via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Apr 7 01:27:23 PDT 2016


On Thursday, 7 April 2016 at 08:17:38 UTC, Puming wrote:
> On Thursday, 7 April 2016 at 08:07:12 UTC, Edwin van Leeuwen 
> wrote:
>
> OK. Even if it consumes the first two elements, then why does 
> it have to consume them AGAIN when actually used? If the 
> function mkarray has side effects, it could lead to problems.

After some testing it seems to get each element twice, calls 
front on the MapResult twice, on each element. The first two 
mkarray are both for first element, the second two for the 
second. You can solve this by caching the front call with:

xs.map!(x=>mkarray(x)).cache.joiner;


More information about the Digitalmars-d-learn mailing list