[Issue 5466] New: AA ranges

d-bugmail at puremagic.com d-bugmail at puremagic.com
Thu Jan 20 04:08:26 PST 2011


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

           Summary: AA ranges
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: druntime
        AssignedTo: sean at invisibleduck.org
        ReportedBy: bearophile_hugs at eml.cc


--- Comment #0 from bearophile_hugs at eml.cc 2011-01-20 04:06:22 PST ---
I use D associative arrays often so I'd like AAs to be modified so byKey() and
byValue() return ranges (that have a length attribute too) usable with higher
order functions of std.algorithm like map and filter.

AAs too may become iterable with no method calls:
map!q{a * 10}([1:2, 3:4])

I also suggest to add a third associative array member function that returns a
range of (key,value) typecons tuples, as in a similar Python3 dict method.

------------------

To test the situation, this task is to create a dynamic array of pairs (tuples)
like:
[(10,"aa"), (20,"bb"), (30,"cc")]

from the associative array:
[1:'a', 2:'b', 3:'c']

If possible reading things lazily from the associative array.



Idiomatic Python2 solution (iteritems is lazy):

>>> d = {1:'a', 2:'b', 3:'c'}
>>> [(k*10, v*2) for k,v in d.iteritems()]
[(10, 'aa'), (20, 'bb'), (30, 'cc')]



D2 lazy solution without map():

import std.stdio, std.typecons;
void main() {
    auto aa = [1:'a', 2:'b', 3:'c'];
    Tuple!(int, string)[] r;
    foreach (k, v; aa)
        r ~= tuple(k*10, ""~v~v);
    writeln(r);
}



The byPair() range allows to solve the task functionally ad lazily like this:

import std.stdio, std.typecons, std.algorithm;
void main() {
    auto aa = [1:'a', 2:'b', 3:'c'];
    auto r = map!q{ tuple(a[0]*10, a[1]~a[1]) }(aa.byPair());
    writeln(r);
}

-- 
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