[Issue 9868] Hash iteration should support counter variable

d-bugmail at puremagic.com d-bugmail at puremagic.com
Sun Apr 7 04:24:05 PDT 2013


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


Andrej Mitrovic <andrej.mitrovich at gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|                            |WONTFIX


--- Comment #2 from Andrej Mitrovic <andrej.mitrovich at gmail.com> 2013-04-07 04:24:03 PDT ---
Fair enough. Here's a library workaround when it's really needed:

----
import std.stdio;
import std.traits;

struct Walk(Hash)
    if (isAssociativeArray!Hash)
{
    Hash hash;

    int opApply(int delegate(size_t idx, KeyType!Hash key, ValueType!Hash val)
dg)
    {
        int result = 0;

        size_t idx;
        foreach (key, val; hash)
        {
            result = dg(idx++, key, val);
            if (result)
                break;
        }

        return result;
    }
}

auto walk(Hash)(Hash hash)
    if (isAssociativeArray!Hash)
{
    return Walk!Hash(hash);
}

void main()
{
    string[string] hash = ["f" : "foo", "b" : "bar"];

    foreach (i, key, val; hash.walk)
    {
        writefln("%s %s %s", i, key, val);
    }
}
----

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