July 12, 2014 [Issue 13108] New: std.array.walkKeys and std.array.walkValues | ||||
---|---|---|---|---|
| ||||
https://issues.dlang.org/show_bug.cgi?id=13108 Issue ID: 13108 Summary: std.array.walkKeys and std.array.walkValues Product: D Version: D2 Hardware: All OS: All Status: NEW Severity: enhancement Priority: P1 Component: Phobos Assignee: nobody@puremagic.com Reporter: bearophile_hugs@eml.cc I suggest to add to std.array two simple functions that work on associative arrays: walkKeys!func(AA) walkValues!func(AA) They are used to create new associative arrays where you have mapped a given function on the keys or on the values (if you use walkKeys and the mapping of the keys is not distinct, the resulting associative array is smaller, and it's not determinist what value will be kept). So instead of writing this: void main() { import std.stdio, std.array, std.ascii, std.typecons, std.range, std.math; auto aa = ['a': -1, 'b': -2]; aa.byKey.zip(aa.byValue) .map!(kv => tuple(kv[0].toUpper, kv[1])) .assocArray.writeln; // ['A':-1, 'B':-2] aa.byKey.zip(aa.byValue) .map!(kv => tuple(kv[0], kv[1].abs)) .assocArray.writeln; // ['a':1, 'b':2] } You can write just: void main() { import std.stdio, std.array, std.ascii, std.typecons, std.range, std.math; auto aa = ['a': -1, 'b': -2]; aa.walkKeys!toUpper.writeln; // ['A':-1, 'B':-2] aa.walkValues!abs.writeln; // ['a':1, 'b':2] } Note that the D specs don't specify that "zip(aa.byKey, aa.byValue)" is correct. -- |
Copyright © 1999-2021 by the D Language Foundation