www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Error in 'The D Programming Language' (2010)?

reply asdfa <k88a314 gmail.com> writes:
I have copied more or less verbatim an example from The D 
Programming Language, under 1.4.3 Counting Frequencies. Lambda 
Functions

This is the code

import std.stdio,
        std.string;

void main()
{
   uint[string] freqs;
   // Compute counts
   foreach (line; stdin.byLine())
   {
     foreach (word; split(strip(line)))
     {
       ++freqs[word.idup];
     }
   }

   // Print counts
   string[] words = freqs.keys;
   sort!((a, b) { return freqs[a] > freqs[b]; })(words); // won't 
compile ????
   foreach (word; words)
   {
     writefln("%6u\t%s", freqs[word], words);
   }
}

Both DMD and GDC complain, saying
Error: template instance sort!((a, b)
{
return freqs[a] > freqs[b];
}
) template 'sort' is not defined


Have I made a mistake, or has the D syntax perhaps changed since 
the book was published?
I don't understand this functional voodoo stuff, so I don't have 
the faintest clue how to fix it myself

Anyone know how to fix this?
Jan 11
parent reply =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 01/11/2016 04:28 PM, asdfa wrote:

 Both DMD and GDC complain, saying
 Error: template instance sort!((a, b)
 {
 return freqs[a] > freqs[b];
 }
 ) template 'sort' is not defined
That issue is already in the errata: http://erdani.com/tdpl/errata/ Add the following line to fix: import std.algorithm; Ali
Jan 11
parent asdfa <k88a314 gmail.com> writes:
On Tuesday, 12 January 2016 at 00:36:15 UTC, Ali Çehreli wrote:
 On 01/11/2016 04:28 PM, asdfa wrote:

 Both DMD and GDC complain, saying
 Error: template instance sort!((a, b)
 {
 return freqs[a] > freqs[b];
 }
 ) template 'sort' is not defined
That issue is already in the errata: http://erdani.com/tdpl/errata/ Add the following line to fix: import std.algorithm; Ali
Thank you so much! It works now well it does after I changed writefln("%6u\t%s", freqs[word], words); to writefln("%6u\t%s", freqs[word], word); (stupid mistake I made copying)
Jan 11