Making associatvie array from array of pairs

bearophile bearophileHUGS at lycos.com
Tue Dec 24 04:12:43 PST 2013


Philippe Sigaud:

>     auto a = [["1","0000FF"], ["2", "00FF00"]];
>     import std.algorithm: reduce;
>    string[string] b;
>     b = reduce!((aa, pair) { aa[pair[0]] = pair[1]; return 
> aa;})(b,a);
>     writeln(b);

While this code seems correct (and I think it's kind of common in 
Scala), I consider it an obfuscation to avoid. A normal foreach 
loop is much more readable and should be preferred for this. 
Functional-style programming is not always the best.

Bye,
bearophile


More information about the Digitalmars-d-learn mailing list