Jump to page: 1 2
Thread overview
Making associatvie array from array of pairs
Dec 24, 2013
Dfr
Dec 24, 2013
Gary Willoughby
Dec 24, 2013
Philippe Sigaud
Dec 24, 2013
bearophile
Dec 24, 2013
Philippe Sigaud
Dec 24, 2013
monarch_dodra
Dec 24, 2013
Philippe Sigaud
Dec 24, 2013
Timon Gehr
Dec 25, 2013
Dfr
Dec 25, 2013
bearophile
Dec 25, 2013
Dfr
December 24, 2013
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", ...];


December 24, 2013
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.

http://dlang.org/phobos/std_array.html#.assocArray
December 24, 2013
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);
December 24, 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
December 24, 2013
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];
December 24, 2013
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.
December 24, 2013
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);
}

December 24, 2013
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.
December 25, 2013
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[][])[])'
December 25, 2013
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
« First   ‹ Prev
1 2