Writing generator functions with the yield statement
Most of the recipes we've looked at have been designed to work with all of the items in a single collection. The approach has been to use a for
statement to step through each item within the collection, either mapping the value to a new item, or reducing the collection to some kind of summary value.
Producing a single result from a collection is one of two ways to work with a collection. The alternative is to produce incremental results instead of a single result.
This approach is very helpful in the cases where we can't fit an entire collection in memory. For example, analyzing gigantic web log files is best done in small doses rather than by creating an in-memory collection.
Is there some way to disentangle the collection structure from the processing function? Can we yield results from processing as soon as each individual item is available?
Getting ready
We'll look at some web log data that has date-time string values. We need to parse...