Thread overview | |||||
---|---|---|---|---|---|
|
January 12, 2016 Error in 'The D Programming Language' (2010)? | ||||
---|---|---|---|---|
| ||||
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? |
January 11, 2016 Re: Error in 'The D Programming Language' (2010)? | ||||
---|---|---|---|---|
| ||||
Posted in reply to asdfa | 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 |
January 12, 2016 Re: Error in 'The D Programming Language' (2010)? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ali Çehreli | 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)
|
Copyright © 1999-2021 by the D Language Foundation