Knuth and McIlroy’s Word Count Programs – Java 8 Version

Franklin Chen wrote an interesting article about how Donald Knuth was asked to write a word count program using literate programming and how Doug McIlroy wrote an equivalent one-liner using the Unix shell. Franklin then goes on to rewrite the program in Haskell. As an exercise I have written my own version of the program using Java 8 lambdas and streams. The guts of the program is a (long) one-liner. Here is my version: It’s not as straightforward as the shell version, but still a nice example of the power of the new Java 8 features.