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?

Reply via email to