groupBy in D?

Russel Winder via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Mar 14 10:05:46 PDT 2017


On Tue, 2017-03-14 at 16:35 +0000, Russel Winder wrote:
> 

So by analogy with the Python code I get the following D code, but it
seems ugly compared to the niceness of the Groovy code (and equivalent
Kotlin and Ceylon codes):

import std.array: array, split;
import std.algorithm: filter, map;
import std.file: dirEntries, SpanMode;
import std.path: baseName, dirName;
import std.stdio: writeln;
import std.typecons: Tuple, tuple;


	auto files = dirEntries(path, SpanMode.shallow)
	 .filter!(a => a.isFile)
	 .map!(a => a.baseName)
	 .map!(a => tuple(a.split('_')[0], a))
	 .array;
	writeln(files);
	string[][string] groups;
	foreach (Tuple!(string, string) f; files) {
		groups[f[0]] ~= [f[1]];
	}



-- 
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/20170314/7949590e/attachment.sig>


More information about the Digitalmars-d-learn mailing list