Re: Is there a more elegant way to do this in D?

2021-04-11 Thread Q. Schroll via Digitalmars-d-learn
On Thursday, 8 April 2021 at 18:06:25 UTC, Meta wrote: On Thursday, 8 April 2021 at 18:01:56 UTC, Meta wrote: On Thursday, 8 April 2021 at 12:19:29 UTC, WebFreak001 wrote: ```d string to01String(int[] x) @safe { auto conv = x.to!(ubyte[]); // allocates new array, so later cast to string i

Re: Is there a more elegant way to do this in D?

2021-04-09 Thread ddcovery via Digitalmars-d-learn
On Thursday, 8 April 2021 at 04:02:26 UTC, Ali Çehreli wrote: On 4/7/21 8:57 PM, Brad wrote:     auto a = [1,0,1,1,1,0,1,0,1,1,1,1,0]; I want to come out of this with a string that looks like this: 101110100 Me, me, me, me! :) import std; void main() { auto a = [1,0,1,1,1,0,1,0,1,1

Re: Is there a more elegant way to do this in D?

2021-04-09 Thread Imperatorn via Digitalmars-d-learn
On Thursday, 8 April 2021 at 22:27:38 UTC, Alain De Vos wrote: So which concrete types do you give for the two auto's. Like Paul said. But if you really wanted to type it out: a is int[], conv is ubyte[] and the map is lazy, so add .array and it evaluates to char[]

Re: Is there a more elegant way to do this in D?

2021-04-08 Thread Paul Backus via Digitalmars-d-learn
On Thursday, 8 April 2021 at 22:27:38 UTC, Alain De Vos wrote: So which concrete types do you give for the two auto's. The first `auto` is the return type of `to!(ubyte[])`--so, it's `ubyte[]`. The second `auto` is the return type of `map`. If you look at the documentation [2], you'll see t

Re: Is there a more elegant way to do this in D?

2021-04-08 Thread Alain De Vos via Digitalmars-d-learn
So which concrete types do you give for the two auto's.

Re: Is there a more elegant way to do this in D?

2021-04-08 Thread Imperatorn via Digitalmars-d-learn
On Thursday, 8 April 2021 at 22:02:47 UTC, Alain De Vos wrote: I resume in the 4 ways presented, import std; void main(){ auto a=[1,0,1,1,1,0,1,0,1,1,1,0]; string s = format!"%-(%s%)"(a); writeln(s); dchar[12] b = a.map!(to!string).joiner.array;

Re: Is there a more elegant way to do this in D?

2021-04-08 Thread Alain De Vos via Digitalmars-d-learn
I resume in the 4 ways presented, import std; void main(){ auto a=[1,0,1,1,1,0,1,0,1,1,1,0]; string s = format!"%-(%s%)"(a); writeln(s); dchar[12] b = a.map!(to!string).joiner.array; writeln(b); auto conv = a.to!(ubyte[]);

Re: Is there a more elegant way to do this in D?

2021-04-08 Thread H. S. Teoh via Digitalmars-d-learn
On Thu, Apr 08, 2021 at 08:28:44PM +, Alain De Vos via Digitalmars-d-learn wrote: > The ascii code of 0 is 48 so I think you can add everywhere 48 (but > I'm not a specialist) Why bother with remembering it's 48? Just add '0', like this: int a = [1, 0, 1, 0, 1, ...]; string s

Re: Is there a more elegant way to do this in D?

2021-04-08 Thread Alain De Vos via Digitalmars-d-learn
The ascii code of 0 is 48 so I think you can add everywhere 48 (but I'm not a specialist)

Re: Is there a more elegant way to do this in D?

2021-04-08 Thread Meta via Digitalmars-d-learn
On Thursday, 8 April 2021 at 18:01:56 UTC, Meta wrote: On Thursday, 8 April 2021 at 12:19:29 UTC, WebFreak001 wrote: ```d string to01String(int[] x) @safe { auto conv = x.to!(ubyte[]); // allocates new array, so later cast to string is OK conv[] += '0'; // assume all numbers are 0-9, t

Re: Is there a more elegant way to do this in D?

2021-04-08 Thread Meta via Digitalmars-d-learn
On Thursday, 8 April 2021 at 12:19:29 UTC, WebFreak001 wrote: ```d string to01String(int[] x) @safe { auto conv = x.to!(ubyte[]); // allocates new array, so later cast to string is OK conv[] += '0'; // assume all numbers are 0-9, then this gives the correct result return (() @trus

Re: Is there a more elegant way to do this in D?

2021-04-08 Thread Jack via Digitalmars-d-learn
On Thursday, 8 April 2021 at 16:45:14 UTC, Jack wrote: On Thursday, 8 April 2021 at 04:02:26 UTC, Ali Çehreli wrote: On 4/7/21 8:57 PM, Brad wrote:     auto a = [1,0,1,1,1,0,1,0,1,1,1,1,0]; I want to come out of this with a string that looks like this: 101110100 Me, me, me, me! :) i

Re: Is there a more elegant way to do this in D?

2021-04-08 Thread Jack via Digitalmars-d-learn
On Thursday, 8 April 2021 at 04:02:26 UTC, Ali Çehreli wrote: On 4/7/21 8:57 PM, Brad wrote:     auto a = [1,0,1,1,1,0,1,0,1,1,1,1,0]; I want to come out of this with a string that looks like this: 101110100 Me, me, me, me! :) import std; void main() { auto a = [1,0,1,1,1,0,1,0,1,1

Re: Is there a more elegant way to do this in D?

2021-04-08 Thread WebFreak001 via Digitalmars-d-learn
On Thursday, 8 April 2021 at 03:57:23 UTC, Brad wrote: I am trying to take an array and convert it to a string. I know that Split will let me easily go the other way. I searched for the converse of Split but have not been able to locate it. I can think of two brute force methods of doing th

Re: Is there a more elegant way to do this in D?

2021-04-07 Thread Ali Çehreli via Digitalmars-d-learn
On 4/7/21 8:57 PM, Brad wrote:     auto a = [1,0,1,1,1,0,1,0,1,1,1,1,0]; I want to come out of this with a string that looks like this: 101110100 Me, me, me, me! :) import std; void main() { auto a = [1,0,1,1,1,0,1,0,1,1,1,1,0]; string s = format!"%-(%s%)"(a); writeln(s); } Al