List Comprehension equivalent

John Colvin via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Mar 17 12:21:50 PDT 2017


On Friday, 17 March 2017 at 19:08:36 UTC, Russel Winder wrote:
> On Fri, 2017-03-17 at 17:51 +0000, Jerry via 
> Digitalmars-d-learn wrote:
>> On Friday, 17 March 2017 at 17:13:48 UTC, Russel Winder wrote:
>> > I have a bit of code:
>> > 
>> > 	string[] returnValue;
>> > 	foreach(string key, string[] value; groups) {
>> > 		returnValue ~=
>> > value.sort!debianPackageNumberComparator()[0..$-1].array;
>> > 	}
>> > 	return returnValue;
>> > 
>> > [...]
>> 
>> You forgot a ! on the map call.
>> 
>> .map!((Tuple!(string, string[]) a) =>
>> a[1].sort!debianPackageNumberComparator()[0..$-1])
>
> How embarrassed am I? :-)
>
> However, the result of the map appears to be untransformable to
> anything related to a sequence of string. The result is some 
> type that
> renders as a sequence of sequence using writeln but how to 
> reduce it to
> a sequence? reduce isn't defined on MapResult and if I 
> transform to an
> array reduce is not defined on SortedRange[]. Rust ownership 
> problems
> seem to be a doddle compared to this problem.

reduce is a free function in std.algorithm. Just import it and 
you're away. Anyway, is this what you wanted?

string[] blah(string[][string] groups)
{
     import std.algorithm : map, joiner;
     import std.array : array, byPair;
     return groups.byPair()
         .map!(a => 
a[1].sort!debianPackageNumberComparator()[0..$-1])
         .joiner
         .array;
}


More information about the Digitalmars-d-learn mailing list