Functional Python Programming

Create succinct and expressive implementations with functional programming in Python

Functional Python Programming

This ebook is included in a Mapt subscription
Steven F. Lott

1 customer reviews
Create succinct and expressive implementations with functional programming in Python
$10.00
$49.99
RRP $29.99
RRP $49.99
eBook
Print + eBook
Preview in Mapt

Book Details

ISBN 139781784396992
Paperback360 pages

Book Description

Python is an easy-to-learn and extensible programming language that offers a number of functional programming features. It's ideally suited to a number of applications in the broad space of data science.

This practical guide demonstrates the Python implementation of a number of functional programming techniques and design patterns. Starting with a general overview of functional programming concepts, you will explore common functional features such as first-class and higher-order functions, pure functions and more, and how these are accomplished in Python. Additionally, you will cover how common functional optimizations can be handled in Python. You'll also explore data preparation techniques and data exploration in depth. Moving on, you will learn how the Python standard library fits the functional programming model. The book concludes with a look at the PyMonad project and some larger examples.

By the end of this book, you will be able to understand what functional programming is all about, its impact on the programming workflow, why it's important, and how to implement it in Python.

Table of Contents

Chapter 1: Introducing Functional Programming
Identifying a paradigm
Subdividing the procedural paradigm
A classic example of functional programming
Exploratory Data Analysis
Summary
Chapter 2: Introducing Some Functional Features
First-class functions
Immutable data
Strict and non-strict evaluation
Recursion instead of a explicit loop state
Functional type systems
Familiar territory
Saving some advanced concepts
Summary
Chapter 3: Functions, Iterators, and Generators
Writing pure functions
Functions as first-class objects
Using strings
Using tuples and namedtuples
Cleaning raw data with generator functions
Using lists, dicts, and sets
Summary
Chapter 4: Working with Collections
An overview of function varieties
Working with iterables
Using zip() to structure and flatten sequences
Using reversed() to change the order
Using enumerate() to include a sequence number
Summary
Chapter 5: Higher-order Functions
Using max() and min() to find extrema
Using Python lambda forms
Lambdas and the lambda calculus
Using the map() function to apply a function to a collection
Using map() with multiple sequences
Using the filter() function to pass or reject data
Using filter() to identify outliers
The iter() function with a sentinel value
Using sorted() to put data in order
Writing higher-order functions
Writing higher-order mappings and filters
Writing generator functions
Building higher-order functions with Callables
Looking at some of the design patterns
Summary
Chapter 6: Recursions and Reductions
Simple numerical recursions
Group-by reductions – from many to fewer
Summary
Chapter 7: Additional Tuple Techniques
Using an immutable namedtuple as a record
Building namedtuples with functional constructors
Avoiding stateful classes by using families of tuples
Polymorphism and Pythonic pattern matching
Summary
Chapter 8: The Itertools Module
Working with the infinite iterators
Using the finite iterators
Cloning iterators with tee()
The itertools recipes
Summary
Chapter 9: More Itertools Techniques
Enumerating the Cartesian product
Reducing a product
Permuting a collection of values
Generating all combinations
Recipes
Summary
Chapter 10: The Functools Module
Function tools
Memoizing previous results with lru_cache
Defining classes with total ordering
Applying partial arguments with partial()
Reducing sets of data with reduce()
Summary
Chapter 11: Decorator Design Techniques
Decorators as higher-order functions
Cross-cutting concerns
Composite design
Adding a parameter to a decorator
Implementing more complex descriptors
Recognizing design limitations
Summary
Chapter 12: The Multiprocessing and Threading Modules
What concurrency really means
The boundary conditions
Sharing resources with process or threads
Where benefits will accrue
Using multiprocessing pools and tasks
Using a multiprocessing pool for concurrent processing
Summary
Chapter 13: Conditional Expressions and the Operator Module
Evaluating conditional expressions
Using the operator module instead of lambdas
Starmapping with operators
Reducing with operators
Summary
Chapter 14: The PyMonad Library
Downloading and installing
Functional composition and currying
Functional composition and the PyMonad multiplication operator
Functors and applicative functors
Monad concepts, the bind() function and the Binary Right Shift operator
Implementing simulation with monads
Additional PyMonad features
Summary
Chapter 15: A Functional Approach to Web Services
The HTTP request-response model
The WSGI standard
Defining web services as functions
Tracking usage
Summary
Chapter 16: Optimizations and Improvements
Memoization and caching
Specializing memoization
Optimizing storage
Optimizing accuracy
Case study – making a chi-squared decision
Computing expected values and displaying a contingency table
Summary

What You Will Learn

  • Use Python's generator functions and generator expressions to work with collections in a non-strict (or lazy) manner
  • Utilize Python library modules including itertools, functools, multiprocessing, and concurrent.futures for efficient functional programs
  • Use Python strings using object-oriented suffix notation and prefix notation
  • Avoid stateful classes with families of tuples
  • Design and implement decorators to create composite functions
  • Use functions like max(), min(), map(), filter(), and sorted()
  • Write higher-order functions

Authors

Table of Contents

Chapter 1: Introducing Functional Programming
Identifying a paradigm
Subdividing the procedural paradigm
A classic example of functional programming
Exploratory Data Analysis
Summary
Chapter 2: Introducing Some Functional Features
First-class functions
Immutable data
Strict and non-strict evaluation
Recursion instead of a explicit loop state
Functional type systems
Familiar territory
Saving some advanced concepts
Summary
Chapter 3: Functions, Iterators, and Generators
Writing pure functions
Functions as first-class objects
Using strings
Using tuples and namedtuples
Cleaning raw data with generator functions
Using lists, dicts, and sets
Summary
Chapter 4: Working with Collections
An overview of function varieties
Working with iterables
Using zip() to structure and flatten sequences
Using reversed() to change the order
Using enumerate() to include a sequence number
Summary
Chapter 5: Higher-order Functions
Using max() and min() to find extrema
Using Python lambda forms
Lambdas and the lambda calculus
Using the map() function to apply a function to a collection
Using map() with multiple sequences
Using the filter() function to pass or reject data
Using filter() to identify outliers
The iter() function with a sentinel value
Using sorted() to put data in order
Writing higher-order functions
Writing higher-order mappings and filters
Writing generator functions
Building higher-order functions with Callables
Looking at some of the design patterns
Summary
Chapter 6: Recursions and Reductions
Simple numerical recursions
Group-by reductions – from many to fewer
Summary
Chapter 7: Additional Tuple Techniques
Using an immutable namedtuple as a record
Building namedtuples with functional constructors
Avoiding stateful classes by using families of tuples
Polymorphism and Pythonic pattern matching
Summary
Chapter 8: The Itertools Module
Working with the infinite iterators
Using the finite iterators
Cloning iterators with tee()
The itertools recipes
Summary
Chapter 9: More Itertools Techniques
Enumerating the Cartesian product
Reducing a product
Permuting a collection of values
Generating all combinations
Recipes
Summary
Chapter 10: The Functools Module
Function tools
Memoizing previous results with lru_cache
Defining classes with total ordering
Applying partial arguments with partial()
Reducing sets of data with reduce()
Summary
Chapter 11: Decorator Design Techniques
Decorators as higher-order functions
Cross-cutting concerns
Composite design
Adding a parameter to a decorator
Implementing more complex descriptors
Recognizing design limitations
Summary
Chapter 12: The Multiprocessing and Threading Modules
What concurrency really means
The boundary conditions
Sharing resources with process or threads
Where benefits will accrue
Using multiprocessing pools and tasks
Using a multiprocessing pool for concurrent processing
Summary
Chapter 13: Conditional Expressions and the Operator Module
Evaluating conditional expressions
Using the operator module instead of lambdas
Starmapping with operators
Reducing with operators
Summary
Chapter 14: The PyMonad Library
Downloading and installing
Functional composition and currying
Functional composition and the PyMonad multiplication operator
Functors and applicative functors
Monad concepts, the bind() function and the Binary Right Shift operator
Implementing simulation with monads
Additional PyMonad features
Summary
Chapter 15: A Functional Approach to Web Services
The HTTP request-response model
The WSGI standard
Defining web services as functions
Tracking usage
Summary
Chapter 16: Optimizations and Improvements
Memoization and caching
Specializing memoization
Optimizing storage
Optimizing accuracy
Case study – making a chi-squared decision
Computing expected values and displaying a contingency table
Summary

Book Details

ISBN 139781784396992
Paperback360 pages
Read More
From 1 reviews

Read More Reviews