digitalmars.D.learn - Error in 'The D Programming Language' (2010)?
- asdfa (37/37) Jan 11 2016 I have copied more or less verbatim an example from The D
- =?UTF-8?Q?Ali_=c3=87ehreli?= (6/12) Jan 11 2016 That issue is already in the errata:
- asdfa (7/19) Jan 11 2016 Thank you so much! It works now
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 2016
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 definedThat issue is already in the errata: http://erdani.com/tdpl/errata/ Add the following line to fix: import std.algorithm; Ali
Jan 11 2016
On Tuesday, 12 January 2016 at 00:36:15 UTC, Ali Çehreli wrote:On 01/11/2016 04:28 PM, asdfa wrote: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)Both DMD and GDC complain, saying Error: template instance sort!((a, b) { return freqs[a] > freqs[b]; } ) template 'sort' is not definedThat issue is already in the errata: http://erdani.com/tdpl/errata/ Add the following line to fix: import std.algorithm; Ali
Jan 11 2016