You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
The text was updated successfully, but these errors were encountered:
So summarizing the status quo in D is:
---
auto names2 = ["Sam", "Pamela", "Dave", "Pascal", "Erik"];
auto nameRange2 = names2.enumerate.filter!(a => a.value.length <= a.index).map!(a => a.value);
nameRange2.writeln;
---https://run.dlang.io/is/SzLRES
It's not too bad and I doubt that filterIndex would be accepted.
bearophile_hugs reported this on 2013-03-30T14:06:44Z
Transfered from https://issues.dlang.org/show_bug.cgi?id=9841
CC List
Description
The text was updated successfully, but these errors were encountered: