digitalmars.D.learn - Making associatvie array from array of pairs
- Dfr (4/4) Dec 24 2013 Let's say i have array of kind:
- Gary Willoughby (4/8) Dec 24 2013 You can if the initial array was comprised of tuples instead of
- Philippe Sigaud (8/12) Dec 24 2013 To build a value (your b) from a range (a), you can use
- bearophile (7/13) Dec 24 2013 While this code seems correct (and I think it's kind of common in
- Philippe Sigaud (12/22) Dec 24 2013 I don't know Scala much (I find the code it makes me write is far too
- monarch_dodra (8/13) Dec 24 2013 you use map and filter to reduce a structure down to a value? Or
- Philippe Sigaud (3/8) Dec 24 2013 Something I use often. Same here, btw: everyday, there are posts code
- Timon Gehr (8/12) Dec 24 2013 void main(){
- Dfr (4/21) Dec 25 2013 This example looks cleanest, but not compile with error:
- bearophile (4/7) Dec 25 2013 It compiles for me.
- Dfr (2/10) Dec 25 2013 Sorry, just forgot to import "std.array", now it works, thank you.
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
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
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
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
On Tue, Dec 24, 2013 at 1:12 PM, bearophile <bearophileHUGS lycos.com> wrote:Philippe Sigaud: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 :-)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)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
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
On Tue, Dec 24, 2013 at 3:07 PM, monarch_dodra <monarchdodra gmail.com> wrote:Something I use often. Same here, btw: everyday, there are posts code with map in it.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?
Dec 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); }
Dec 24 2013
On Tuesday, 24 December 2013 at 14:39:16 UTC, Timon Gehr wrote:On 12/24/2013 12:36 PM, Dfr wrote:This example looks cleanest, but not compile with error: Error: no property 'assocArray' for type 'MapResult!(__lambda9, immutable(char[][])[])'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 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
Dec 25 2013
On Wednesday, 25 December 2013 at 14:44:57 UTC, bearophile wrote:Dfr:Sorry, just forgot to import "std.array", now it works, thank you.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