On Monday, 2 October 2023 at 02:47:37 UTC, Joel wrote:
```d
import std;
auto data=“I went for a walk, and fell down a hole.”;
void main(string[] args) {
int[string] dic;
struct WordCnt {
string word;
ulong count;
string toString() const {
return text("Word: ", word, " - number of
instances: ", count);
}
}
WordCnt[] wc;
data
.map!(c => lowercase.canFind(std.uni.toLower(c)) ? c :
' ')
.to!string
.splitter
.each!(d => dic[d]+=1);
foreach(key, value; dic)
wc~=WordCnt(key, value);
wc.sort!"a.count>b.count".each!writeln;
}
```
How can I improve this code? Like avoiding using foreach.
You don't need a struct at all, you can just have an int[string]
aa