String joining an array of structs or class instances implementing toString?

Edwin van Leeuwen via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Feb 11 05:54:34 PST 2016


On Thursday, 11 February 2016 at 13:43:49 UTC, pineapple wrote:
> Thanks! Does the map function iterate without constructing an 
> extra list in-memory?

Yes, it is lazy, so it only calls toString when the result is 
actually used (by the join call).


In case you do need to create an extra list you can use array as 
follows:

import std.array : array;
auto result = parts.map!((part) => part.toString).array;

The call to array will "force" it to construct an array out of it.




More information about the Digitalmars-d-learn mailing list