www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - join of range of ranges?

reply "bearophile" <bearophileHUGS lycos.com> writes:
In some cases I'd like to join a range of ranges in a single 
array/string (I know here the inner map could be replaced by 
something better, this code is just an example):


import std.algorithm: map;
import std.array: join, array;
void main() {
     auto r1 = [1, 2]
               .map!(x => [1, 2].map!(y => '*').array)
               .join("_");
     auto r2 = [1, 2]
               .map!(x => [1, 2].map!(y => '*'))
               .join("_");
}


The code works only if I add an 'array' inside, to turn it into a 
range of arrays. Do you think it's right to ask as enhancement 
for std.array.join to work with a range of ranges too, as in the 
r2 case?

Bye,
bearophile
Sep 22 2013
next sibling parent reply "David Nadlinger" <code klickverbot.at> writes:
On Sunday, 22 September 2013 at 14:26:14 UTC, bearophile wrote:
 In some cases I'd like to join a range of ranges in a single 
 array/string (I know here the inner map could be replaced by 
 something better, this code is just an example):
std.algorithm.joiner, or am I missing something? David
Sep 22 2013
parent "bearophile" <bearophileHUGS lycos.com> writes:
David Nadlinger:

 std.algorithm.joiner, or am I missing something?
Something like this? (It doesn't work yet): import std.algorithm: map, joiner; import std.array: join, array; import std.string: text; void main() { string r1 = [1, 2] .map!(x => [1, 2].map!(y => '*').array) .join("_"); string r2 = [1, 2] .map!(x => [1, 2].map!(y => '*')) .joiner("_") .text; } Bye, bearophile
Sep 22 2013
prev sibling parent reply "Peter Alexander" <peter.alexander.au gmail.com> writes:
On Sunday, 22 September 2013 at 14:26:14 UTC, bearophile wrote:
     auto r2 = [1, 2]
               .map!(x => [1, 2].map!(y => '*'))
               .join("_");
The problem is that you are trying to map a range of range of chars with a range of dchars. auto r2 = [1, 2] .map!(x => [1, 2].map!(y => cast(dchar)'*')) .join("_"); This works. I really wish character literals in D where always dchar.
Sep 22 2013
next sibling parent reply "bearophile" <bearophileHUGS lycos.com> writes:
Peter Alexander:

 The problem is that you are trying to map a range of range of 
 chars with a range of dchars.

 auto r2 = [1, 2]
               .map!(x => [1, 2].map!(y => cast(dchar)'*'))
               .join("_");

 This works.
I see, thank you. When I ask a question it seems my brain switches off a bit :-)
 I really wish character literals in D where always dchar.
This is supported: void main() { auto s1 = "hello"w; auto s2 = "hello"d; } So, what about adding support for this? void main() { auto c1 = 'X'w; auto c2 = 'X'd; static assert(is(typeof(c1) == wchar)); static assert(is(typeof(c2) == dchar)); } Bye, bearophile
Sep 22 2013
parent reply "monarch_dodra" <monarchdodra gmail.com> writes:
On Sunday, 22 September 2013 at 20:27:01 UTC, bearophile wrote:
 Peter Alexander:

 The problem is that you are trying to map a range of range of 
 chars with a range of dchars.

 auto r2 = [1, 2]
              .map!(x => [1, 2].map!(y => cast(dchar)'*'))
              .join("_");

 This works.
I see, thank you. When I ask a question it seems my brain switches off a bit :-)
 I really wish character literals in D where always dchar.
This is supported: void main() { auto s1 = "hello"w; auto s2 = "hello"d; } So, what about adding support for this? void main() { auto c1 = 'X'w; auto c2 = 'X'd; static assert(is(typeof(c1) == wchar)); static assert(is(typeof(c2) == dchar)); } Bye, bearophile
Even "hello"c is supported actually :) It can make a difference if you *don't* want your string implicitly promoted on declaration. EG: dstring ds1 = "hello"; //Fine dstring ds2 = "hello"c; //Nope. As for allowing 'X'w, I think the rationale is that a cast will get you the same result (not so with string literals).
Sep 22 2013
parent "bearophile" <bearophileHUGS lycos.com> writes:
monarch_dodra:

 As for allowing 'X'w, I think the rationale is that a cast will 
 get you the same result (not so with string literals).
OK. I have desired those char suffixes for years, so now I have written an ER: http://d.puremagic.com/issues/show_bug.cgi?id=11103 Bye, bearophile
Sep 22 2013
prev sibling parent "Peter Alexander" <peter.alexander.au gmail.com> writes:
On Sunday, 22 September 2013 at 18:13:39 UTC, Peter Alexander 
wrote:
 On Sunday, 22 September 2013 at 14:26:14 UTC, bearophile wrote:
    auto r2 = [1, 2]
              .map!(x => [1, 2].map!(y => '*'))
              .join("_");
The problem is that you are trying to map a range of range of chars with a range of dchars.
This could also be solved by having join return an array of the CommonType of the elements of both ranges.
Sep 22 2013