An asynchronous example
To keep things simple but still interesting, let's write a tool that, given a text file, will count the occurrences of a given word. This example builds on the silly coroutine that we implemented in the previous section, adding some useful behavior to it.
It should be noted that, at least on a Linux or Mac OS X machine, one can achieve the same result very simply using the grep
command, as we will see. Let's start by downloading a significantly large text that we will use as input data for our experiments. Let's just choose a public domain book from Project Gutenberg: War and Peace by Leo Tolstoy, which is freely available at http://www.gutenberg.org/cache/epub/2600/pg2600.txt.
The following snippet shows how we can download this text very easily:
$ curl -sO http://www.gutenberg.org/cache/epub/2600/pg2600.txt $ wc pg2600.txt 65007 566320 3291648 pg2600.txt
Next, we will start by counting the number of occurrences of the word love, regardless of case, in the file...