Comment #0 by bearophile_hugs — 2014-07-12T12:13:36Z
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.