Let’s first write a snippet to count the word occurences from a paragraph, in of course a functional way.
Run this example and you’ll get a list of fruits, along with their counts:
This example includes most aspects of processing collections with FP style. For instance,
re.finditer returns an
iterator that is lazily evaluated;
filter are used to do transformations;
itertools module provides various functions to cope with iterables; and last but not least, the
lambda expression, an easy way to define inline anonymous function. All of them will be described in the following sections.