Golfing a word count program

In “Can C++ become your new scripting language?”, I put together a couple of short programs to compare some of the more modern features of C++ with some of the conveniences of using Perl.

The post got some attention, but some seemed to miss the forest for the trees. The point was not to come up with the shortest possible word counting program in either language. Rather, I constructed a somewhat artificially complicated example to illustrate a small number of the great conveniences modern C++ provides.

Some got hung up on the fact the Perl script is wrapped in run function. This is a useful thing to do if you want to avoid accidentally introducing global variables to your program.

Yes, both programs first create a frequency distribution and then sum the counts in that distribution. Clearly, you don’t need to do that, but my purpose was to come with a way, however artificial, of illustrating the fact that I did not have to initialize the counts in the C++ map. If you are immersed in the C mindset, this should be earth-shattering.

Anyway, of course, if you want to golf the task of counting whitespace separated strings of non-whitespace characters, you can do it in Perl using the simple one-liner: perl -E ‘$x += split while <>; say $x’ will do the job.

You are just going to write C++ like that. And, once a Perl one-liner becomes a Perl five-liner, you might as well put in a few lines of scaffolding to prepare for the time it becomes a five-hundred-liner, and then a five-thousand-liner upon which your business relies.

Most interesting Perl programs are not one-liners. They free the programmer from the drudgery of building up and tearing down commonly used data structures to enhance the programmer’s ability to focus on the real problem. It is refreshing to see that C++ now feels that way as well.