Copyright and Credits
Preface
Chapter 1: Understanding Functional Programming
Identifying a paradigm
Subdividing the procedural paradigm
Using the functional paradigm
Using a functional hybrid
Looking at object creation
The stack of turtles
A classic example of functional programming
Exploratory data analysis
Summary
Chapter 2: Introducing Essential Functional Concepts
First-class functions
Pure functions
Higher-order functions
Immutable data
Strict and non-strict evaluation
Recursion instead of an explicit loop state
Functional type systems
Familiar territory
Learning some advanced concepts
Summary
Chapter 3: Functions, Iterators, and Generators
Writing pure functions
Functions as first-class objects
Using strings
Using tuples and named tuples
Using generator expressions
Exploring the limitations of generators
Combining generator expressions
Cleaning raw data with generator functions
Using lists, dicts, and sets
Using stateful mappings
Using the bisect module to create a mapping
Using stateful sets
Summary
Chapter 4: Working with Collections
An overview of function varieties
Working with iterables
Parsing an XML file
Parsing a file at a higher level
Pairing up items from a sequence
Using the iterO function explicitly
Extending a simple loop
Applying generator expressions to scalar functions
Using any() and all() as reductions
Using lenO and sum()
Using sums and counts for statistics
Using zip() to structure and flatten sequences
Unzipping a zipped sequence
Flattening sequences
Structuring flat sequences
Structuring flat sequences - an alternative approach
Using reversed() to change the order
Using enumerate() to include a sequence number
Summary
Chapter 5: Higher-Order Functions
Using max() and min0 to find extrema
Using Python lambda forms
Lambdas and the lambda calculus
Using the map() function to apply a function to a collection
Working with lambda forms and map()
Using map() with multiple sequences
Using the filter() function to pass or reject data
Using filter() to identify outliers
The iter0 function with a sentinel value
Using sorted() to put data in order
Writing higher-order functions
Writing higher-order mappings and filters
Unwrapping data while mapping
Wrapping additional data while mapping
Flattening data while mapping
Structuring data while filtering
Writing generator functions
Building higher-order functions with callables
Assuring good functional design
Review of some design patterns
Summary
Chapter 6: Recursions and Reductions
Simple numerical recursions
Implementing tail-call optimization
Leaving recursion in place
Handling difficult tail-call optimization
Processing collections through recursion
Tail-call optimization for collections
Reductions and folding a collection from many items to one
Group-by reduction from many items to fewer
Building a mapping with Counter
Building a mapping by sorting
Grouping or partitioning data by key values
Writing more general group-by reductions
Writing higher-order reductions
Writing file parsers
Parsing CSV files
Parsing plain text files with headers
Summary
Chapter 7: Additional Tuple Techniques
Using tuples to collect data
Using named tuples to collect data
Building named tuples with functional constructors
Avoiding stateful classes by using families of tuples
Assigning statistical ranks
Wrapping instead of state changing
Rewrapping instead of state changing
Computing Spearman rank-order correlation
Polymorphism and type-pattern matching
Summary
Chapter 8: The Itertools Module
Working with the infinite iterators
Counting with count()
Counting with float ar