www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Making associatvie array from array of pairs

reply "Dfr" <deflexor yandex.ru> writes:
Let's say i have array of kind:

auto a = [["1","0000FF"], ["2", "00FF00"], ...];

Is there simple way to turn it into associative array of kind:

string[string] b = ["1": "0000FF", "2": "00FF00", ...];
Dec 24 2013
next sibling parent "Gary Willoughby" <dev nomad.so> writes:
On Tuesday, 24 December 2013 at 11:36:23 UTC, Dfr wrote:
 Let's say i have array of kind:

 auto a = [["1","0000FF"], ["2", "00FF00"], ...];

 Is there simple way to turn it into associative array of kind:

 string[string] b = ["1": "0000FF", "2": "00FF00", ...];
You can if the initial array was comprised of tuples instead of nested arrays.
Dec 24 2013
prev sibling next sibling parent reply Philippe Sigaud <philippe.sigaud gmail.com> writes:
On Tue, Dec 24, 2013 at 12:36 PM, Dfr <deflexor yandex.ru> wrote:
 Let's say i have array of kind:

 auto a = [["1","0000FF"], ["2", "00FF00"], ...];

 Is there simple way to turn it into associative array of kind:

 string[string] b = ["1": "0000FF", "2": "00FF00", ...];
To build a value (your b) from a range (a), you can use std.algorithm.reduce, it's a very generic algo: 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);
Dec 24 2013
parent reply "bearophile" <bearophileHUGS lycos.com> writes:
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
Dec 24 2013
parent reply Philippe Sigaud <philippe.sigaud gmail.com> writes:
On Tue, Dec 24, 2013 at 1:12 PM, bearophile <bearophileHUGS lycos.com> wrote:
 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 don't know Scala much (I find the code it makes me write is far too 'heavy', if I may say so). Haskell would be more the origin of my using reduce :-)
 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.
I don't know. I really consider `reduce` a nice way to collapse a structure down to a value. I find it in many different places. Sure, map and filter are more usual, but reduce is not far behind. Anyway... Here with a foreach loop: auto a = [["1","0000FF"], ["2", "00FF00"]]; string[string] b; foreach(pair; a) b[pair[0]] = pair[1];
Dec 24 2013
parent reply "monarch_dodra" <monarchdodra gmail.com> writes:
On Tuesday, 24 December 2013 at 12:33:13 UTC, Philippe Sigaud 
wrote:
 I don't know. I really consider `reduce` a nice way to collapse 
 a
 structure down to a value. I find it in many different places. 
 Sure,
 map and filter are more usual, but reduce is not far behind.
you use map and filter to reduce a structure down to a value? Or do you mean it's just something you use often? I don't think map+filter can be used to "reduce" a range down to a value. It would require a filter with mutable state, and I'm pretty sure doing that means having an implementation defined result.
Dec 24 2013
parent Philippe Sigaud <philippe.sigaud gmail.com> writes:
On Tue, Dec 24, 2013 at 3:07 PM, monarch_dodra <monarchdodra gmail.com> wrote:
 I don't know. I really consider `reduce` a nice way to collapse a
 structure down to a value. I find it in many different places. Sure,
 map and filter are more usual, but reduce is not far behind.
you use map and filter to reduce a structure down to a value? Or do you mean it's just something you use often?
Something I use often. Same here, btw: everyday, there are posts code with map in it.
Dec 24 2013
prev sibling parent reply Timon Gehr <timon.gehr gmx.ch> writes:
On 12/24/2013 12:36 PM, Dfr wrote:
 Let's say i have array of kind:

 auto a = [["1","0000FF"], ["2", "00FF00"], ...];

 Is there simple way to turn it into associative array of kind:

 string[string] b = ["1": "0000FF", "2": "00FF00", ...];
void main(){ import std.array, std.algorithm, std.typecons; auto a = [["1","0000FF"], ["2", "00FF00"], /+...+/]; auto aa = a.map!(x=>tuple(x[0],x[1])).assocArray; import std.stdio; writeln(aa); }
Dec 24 2013
parent reply "Dfr" <deflexor yandex.ru> writes:
On Tuesday, 24 December 2013 at 14:39:16 UTC, Timon Gehr wrote:
 On 12/24/2013 12:36 PM, Dfr wrote:
 Let's say i have array of kind:

 auto a = [["1","0000FF"], ["2", "00FF00"], ...];

 Is there simple way to turn it into associative array of kind:

 string[string] b = ["1": "0000FF", "2": "00FF00", ...];
void main(){ import std.array, std.algorithm, std.typecons; auto a = [["1","0000FF"], ["2", "00FF00"], /+...+/]; auto aa = a.map!(x=>tuple(x[0],x[1])).assocArray; import std.stdio; writeln(aa); }
This example looks cleanest, but not compile with error: Error: no property 'assocArray' for type 'MapResult!(__lambda9, immutable(char[][])[])'
Dec 25 2013
parent reply "bearophile" <bearophileHUGS lycos.com> writes:
Dfr:

 This example looks cleanest, but not compile with error:

 Error: no property 'assocArray' for type 'MapResult!(__lambda9, 
 immutable(char[][])[])'
It compiles for me. Bye, bearophile
Dec 25 2013
parent "Dfr" <deflexor yandex.ru> writes:
On Wednesday, 25 December 2013 at 14:44:57 UTC, bearophile wrote:
 Dfr:

 This example looks cleanest, but not compile with error:

 Error: no property 'assocArray' for type 
 'MapResult!(__lambda9, immutable(char[][])[])'
It compiles for me. Bye, bearophile
Sorry, just forgot to import "std.array", now it works, thank you.
Dec 25 2013