Simplest way to create an array from an associative array which its contains keys and values?

Craig Dillabaugh cdillaba at cg.scs.carleton.ca
Tue Jan 7 12:38:10 PST 2014


On Friday, 3 January 2014 at 17:38:16 UTC, Gary Willoughby wrote:
> Simplest way to create an array from an associative array which 
> its contains keys and values?
>
> For example if i have an associative array like this:
>
> ["one":"1", "two":"2"]
>
> What's the easiest way to create a dynamic array that looks 
> like this:
>
> ["one", "1", "two", "2"]
>
> I know it can be done via a loop, but is there a more idiomatic 
> way to achieve this?

As someone with little experience with functional programming, I 
am just curious - having browsed through the thread - if the 
various solutions proposed here would really be considered more 
'idiomatic' D. Or if they were posted because the OP asked about 
avoiding the foreach() loop.

In other words while:

     auto range = aa.byKey.map!(a => chain(a.only, aa[a].only));
     string[] array = range.join;

Saves a few lines of code, and looks cooler, it seems that the 
trivial foreach loop version is very easy:

string[] array;

foreach (key, value; aa) {
	array ~= key;
	array ~= value;
}




More information about the Digitalmars-d-learn mailing list