| Thread overview | |||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
December 24, 2013 Making associatvie array from array of pairs | ||||
|---|---|---|---|---|
| ||||
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 Re: Making associatvie array from array of pairs | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Dfr | 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 Re: Making associatvie array from array of pairs | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Dfr | 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 Re: Making associatvie array from array of pairs | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Philippe Sigaud | 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 Re: Making associatvie array from array of pairs | ||||
|---|---|---|---|---|
| ||||
Posted in reply to bearophile | 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 Re: Making associatvie array from array of pairs | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Philippe Sigaud | 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 Re: Making associatvie array from array of pairs | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Dfr | 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 Re: Making associatvie array from array of pairs | ||||
|---|---|---|---|---|
| ||||
Posted in reply to monarch_dodra | 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 Re: Making associatvie array from array of pairs | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Timon Gehr | 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 Re: Making associatvie array from array of pairs | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Dfr | 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
| |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply