Thread overview | |||||
---|---|---|---|---|---|
|
March 14, 2017 Re: groupBy in D? | ||||
---|---|---|---|---|
| ||||
Dne 14.3.2017 v 16:02 Russel Winder via Digitalmars-d-learn napsal(a):
> I am having a hard time Google-ing today. Pytho, Groovy, Kotlin, etc.
> have a "groupBy" function that takes a sequence and a key and creates a
> dictionary/associative array with keys the result of the key function
> on the sequence and key values all the items that match the key.
>
> D doesn't seem to have such a function directly. Unless I missed it…
Can you post some example? Maybe in python, kotlin whatever so I can better understand what you want
|
March 14, 2017 Re: groupBy in D? | ||||
---|---|---|---|---|
| ||||
Attachments:
| On Tue, 2017-03-14 at 16:12 +0100, Daniel Kozak via Digitalmars-d-learn wrote: > […] > > Can you post some example? Maybe in python, kotlin whatever so I can better understand what you want In my head I had something such as Groovy's files = new File('test-data').listFiles().collect{it.name} println files.groupBy({ it.split('_')[0]}) The I remembered that itertools.groupby in Python behaves like D's std.algorithm.iteration.chunkBy and not like Groovy's groupBy. So in Python you have to hack it with: from collections import defaultdict from os import listdir from os.path import isfile, splitext result = defaultdict(list) files = tuple((item.split('_')[0], item) for item in listdir('test-data') if isfile('test-data/' + item)) for p, v in files: result[p] += [v] print(result) Given the seeming lack of Groovy-style groupBy, I guess I will have to do something Python-like in D. Except I am not sure D has an equivalent of defaultdict. -- Russel. ============================================================================= Dr Russel Winder t: +44 20 7585 2200 voip: sip:russel.winder@ekiga.net 41 Buckmaster Road m: +44 7770 465 077 xmpp: russel@winder.org.uk London SW11 1EN, UK w: www.russel.org.uk skype: russel_winder |
March 14, 2017 Re: groupBy in D? | ||||
---|---|---|---|---|
| ||||
Attachments:
| 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@ekiga.net 41 Buckmaster Road m: +44 7770 465 077 xmpp: russel@winder.org.uk London SW11 1EN, UK w: www.russel.org.uk skype: russel_winder |
Copyright © 1999-2021 by the D Language Foundation