Another task

bearophile bearophileHUGS at lycos.com
Thu Jan 20 02:12:07 PST 2011


Simen kjaeraas:

> byKey is essentially an opApply. You have to wrap it in a fiber to make it
> work with the range interface:

Thank you for all your code and work.
I have found a bug (it's not a bug of yours): if I compile your code with -release, DMD prints:
test.d(45): Error: function D main is a nested function and cannot be accessed from array
test.d(45): Error: function D main is a nested function and cannot be accessed from array

Where the line 45 is the one that starts with: auto o = map!(...

I use D associative arrays often (it comes from my Python practice), I suggest to modify AAs to change byKey() and byValue() into ranges usable with std.algorithm.
AAs may become iterable with zero method calls too (this is equivalent to byKey or byValue):
map!q{a*10}([1:2, 3:4])

And I suggest to add a third associative array member function that returns a range of key,value tuples, as in Python3. This allows to solve the task like this:
auto r = map!q{ tuple(a[0]*10, a[1]~a[1]) }(aa.byPair());

Bye,
bearophile


More information about the Digitalmars-d mailing list