[Issue 9841] New: std.algorithm.iFilter

d-bugmail at puremagic.com d-bugmail at puremagic.com
Sat Mar 30 14:06:45 PDT 2013


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

           Summary: std.algorithm.iFilter
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: Phobos
        AssignedTo: nobody at puremagic.com
        ReportedBy: bearophile_hugs at eml.cc


--- Comment #0 from bearophile_hugs at eml.cc 2013-03-30 14:06:44 PDT ---
This is a little "challenge" on streams, LINQ, etc:

Challenge 2: Indexed Filtering

Find all the names in the array "names" where the length of the name is less
than or equal to the index of the element + 1.

With LINQ:

string[] names = { "Sam", "Pamela", "Dave", "Pascal", "Erik" };
var nameList = names.Where((c, index) => c.Length <= index + 1).ToList();


In D:

    auto names2 = ["Sam","Pamela", "Dave", "Pascal", "Erik"];
    auto nameRange = iota(size_t.max)
                     .zip(names2)
                     .filter!q{ a[1].length <= a[0] }
                     .map!q{ a[1] };
    nameRange.writeln;


Using enumerate() I have suggested in Issue 5550 the D code improves:

    auto nameRange2 = names2
                      .enumerate
                      .filter!q{ a[1].length <= a[0] }
                      .map!q{ a[1] };
    nameRange2.writeln;


But in this case an iFilter is better than using enumerate+filter+map. In
iFilter the filtering function is supplied by an index+item 2-tuple, so the D
code becomes:

    auto nameRange2 = names2.iFilter!q{ a.length <= i };


So I suggest to add a iFilter or ifilter to Phobos. An alternative name is
filterWithIndex, but iFilter is better because it reminds us that the index is
the fist items of the 2-tuple.

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