Bug 13108 – std.array.walkKeys and std.array.walkValues

Status
NEW
Severity
enhancement
Priority
P4
Component
phobos
Product
D
Version
D2
Platform
All
OS
All
Creation time
2014-07-12T12:13:36Z
Last change time
2024-12-01T16:21:54Z
Assigned to
No Owner
Creator
bearophile_hugs
Moved to GitHub: phobos#10074 →

Comments

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.
Comment #1 by greeenify — 2017-02-19T15:03:45Z
I like the idea, but wouldn't mapKeys or mapValues better names? That's the names, e.g. lodash uses: https://lodash.com/docs/4.17.2#mapKeys https://lodash.com/docs/4.17.2#mapValues
Comment #2 by robert.schadek — 2024-12-01T16:21:54Z
THIS ISSUE HAS BEEN MOVED TO GITHUB https://github.com/dlang/phobos/issues/10074 DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB