digitalmars.D.learn - String joining an array of structs or class instances implementing
- pineapple (16/16) Feb 11 2016 It feels like there should be an out-of-the box way to do this
- pineapple (2/2) Feb 11 2016 Oh pardon the constructor I was playing with as a learning
- Edwin van Leeuwen (5/21) Feb 11 2016 I'd do it like this:
- pineapple (4/8) Feb 11 2016 Thanks! Does the map function iterate without constructing an
- Edwin van Leeuwen (8/10) Feb 11 2016 Yes, it is lazy, so it only calls toString when the result is
- Meta (7/11) Feb 11 2016 You can shorten this by using std.conv.to. Also keep in mind that
- =?UTF-8?Q?Ali_=c3=87ehreli?= (9/25) Feb 11 2016 You can use element format specifiers:
It feels like there should be an out-of-the box way to do this but I haven't been able to find it? Help? This is the thing that I want to do: struct example{ const string str; //this(string str){ this.str = str; } string toString(){ return this.str; } } public void main(){ import std.stdio; import std.string; example[] parts = [example("hello"), example("world")]; writeln(std.string.join(parts, " ")); }
Feb 11 2016
Oh pardon the constructor I was playing with as a learning experience and forgot to get rid of.
Feb 11 2016
On Thursday, 11 February 2016 at 12:44:15 UTC, pineapple wrote:It feels like there should be an out-of-the box way to do this but I haven't been able to find it? Help? This is the thing that I want to do: struct example{ const string str; //this(string str){ this.str = str; } string toString(){ return this.str; } } public void main(){ import std.stdio; import std.string; example[] parts = [example("hello"), example("world")]; writeln(std.string.join(parts, " ")); }I'd do it like this: import std.algorithm : map; pars.map!((part) => part.toString) // Turn them to strings .join(" ").writeln; // Join them.
Feb 11 2016
On Thursday, 11 February 2016 at 12:53:20 UTC, Edwin van Leeuwen wrote:I'd do it like this: import std.algorithm : map; pars.map!((part) => part.toString) // Turn them to strings .join(" ").writeln; // Join them.Thanks! Does the map function iterate without constructing an extra list in-memory?
Feb 11 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.
Feb 11 2016
On Thursday, 11 February 2016 at 12:53:20 UTC, Edwin van Leeuwen wrote:I'd do it like this: import std.algorithm : map; pars.map!((part) => part.toString) // Turn them to strings .join(" ").writeln; // Join them.You can shorten this by using std.conv.to. Also keep in mind that `join` will allocate, but `std.algorithm.joiner` will not. Ex. //No allocations done, not even to!string pars.map!(to!string).joiner(" ").writeln;
Feb 11 2016
On 02/11/2016 04:44 AM, pineapple wrote:It feels like there should be an out-of-the box way to do this but I haven't been able to find it? Help? This is the thing that I want to do: struct example{ const string str; //this(string str){ this.str = str; } string toString(){ return this.str; } } public void main(){ import std.stdio; import std.string; example[] parts = [example("hello"), example("world")]; writeln(std.string.join(parts, " ")); }You can use element format specifiers: writeln(format("%(%s %)", parts)); writefln understands that too: writefln("%(%s %)", parts); Ali [2] http://ddili.org/ders/d.en/formatted_output.html#ix_formatted_output.%%28
Feb 11 2016