Specify variable type for range of associative arrays.

Christopher Davies via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Aug 8 18:29:15 PDT 2015


I'm just learning D. Something I often do in C# is have an 
IEnumerable (Range) of some type that is then conditionally 
filtered. It looks like this:

IEnumerable<Dictionary<string, string>> foo = bar;

if (baz)
{
     foo = foo.Where(d => d["key"] == value);
}

I'm trying to do the same in D. Here's what I want to do:

Range!(string[string]) records = 
csvReader!(string[string])(input, null);

if (where != "")
{
     records = filter!(r => r[where] == val)(records);
}

But Range!(string[string]) isn't right, even though that's what 
the csvReader and filter statements produce. How do I declare 
that type?

I do have a working example, but it's way too verbose (and not 
following D's indentation/bracket style... sorry about that!):

   auto gen = new Generator!(string[string])({
     auto records = csvReader!(string[string])(input, null);

     foreach (record; records) {
       yield(record);
     }
   });

   if (where != "") {
     auto prevGen = gen;
     gen = new Generator!(string[string])({
       auto records = filter!(r => r[where] == val)(prevGen);

       foreach (record; records) {
         yield(record);
       }
     });
   }

Thanks in advance!


More information about the Digitalmars-d-learn mailing list