[Issue 5502] More handy ways to create associative arrays

d-bugmail at puremagic.com d-bugmail at puremagic.com
Wed Feb 6 04:38:08 PST 2013


http://d.puremagic.com/issues/show_bug.cgi?id=5502



--- Comment #2 from bearophile_hugs at eml.cc 2013-02-06 04:38:07 PST ---
Currently if you want to create an associative array with constant values you
have to use something like:


import std.array: assocArray;
import std.range: zip, repeat;
void main() {
    bool[dchar] dcharSet = assocArray(zip("ABCD", repeat(true)));
}



In Python the dict has a static method "fromkeys" that is handy:


>>> dict.fromkeys("ABCD", True)
{'A': True, 'C': True, 'B': True, 'D': True}


A possible similar Phobos function:


import std.array: AAFromKeys;
void main() {
    bool[dchar] dcharSet = AAFromKeys("ABCD", true);
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list