List Comprehension equivalent

Russel Winder via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Mar 17 10:13:48 PDT 2017


I have a bit of code:

	string[] returnValue;
	foreach(string key, string[] value; groups) {
		returnValue ~= value.sort!debianPackageNumberComparator()[0..$-1].array;
	}
	return returnValue;

which does seem to do the thing required, but this sort of code in
Python, Rust, even C++ (but not Go), would be seen as for too "changing
state", not declarative enough. With Python you use list comprehensions
(which are lovely things), in Rust you can do flat_map and collect. I
am fighting in D starting from:

	return groups.byPair()
	 .map((Tuple!(string, string[]) a) => a[1].sort!debianPackageNumberComparator()[0..$-1])
	 .array
	 .joiner;

because the compiler keeps complaining about:


ldc2  '-Isource/approx-gc at exe' '-Isource' '-I../../Repositories/Git/Masters/ApproxGC_D/source' '-enable-color' '-wi' '-O3' '-release'  -of 'source/approx-gc at exe/main.d.o' -c ../../Repositories/Git/Masters/ApproxGC_D/source/main.d
../../Repositories/Git/Masters/ApproxGC_D/source/main.d(75): Error: template std.algorithm.iteration.map cannot deduce function from argument types !()(MapResult!(__lambda2, Result), SortedRange!(string[], debianPackageNumberComparator) function(Tuple!(string, string[]) a) @system), candidates are:
/usr/lib/ldc/x86_64-linux-gnu/include/d/std/algorithm/iteration.d(448):        std.algorithm.iteration.map(fun...) if (fun.length >= 1)

Anyone any idea on how to move forward on this?

-- 
Russel.
=============================================================================
Dr Russel Winder      t: +44 20 7585 2200   voip: sip:russel.winder at ekiga.net
41 Buckmaster Road    m: +44 7770 465 077   xmpp: russel at winder.org.uk
London SW11 1EN, UK   w: www.russel.org.uk  skype: russel_winder
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: This is a digitally signed message part
URL: <http://lists.puremagic.com/pipermail/digitalmars-d-learn/attachments/20170317/9d0ae9e0/attachment.sig>


More information about the Digitalmars-d-learn mailing list