Key and value with ranges

Joel joelcnz at gmail.com
Mon Oct 2 02:47:37 UTC 2023


```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.


More information about the Digitalmars-d-learn mailing list