counting words

monarch_dodra monarchdodra at gmail.com
Fri Jun 28 09:52:35 PDT 2013


On Friday, 28 June 2013 at 16:25:25 UTC, Brad Anderson wrote:
> On Friday, 28 June 2013 at 16:04:35 UTC, Benjamin Thaut wrote:
>> I'm currently making a few tests with std.algorithm, 
>> std.range, etc
>>
>> I have a arry of words. Is it possible to count how often each 
>> word is contained in the array and then sort the array by the 
>> count of the individual words by chaining ranges? (e.g. 
>> without using a foreach loop + hashmap)?
>
> If you don't mind sorting twice:
>
> words.sort()
>      .group()
>      .array()
>      .sort!((a, b)=> a[1] > b[1])
>      .map!(a => a[0])
>      .copy(words);
>
> You could also do it with a hashmap to keep the count.

It's just missing the construction scheme for "words". I had this:

     text
         .splitter();
         .reduce!((words, w)=>++words[w], words)(int[string]);

But alas... reduce's signature is not ctfe-able :(

Well, I've had a PR open for this for about 8 months now...


More information about the Digitalmars-d-learn mailing list