Functional Python Programming - Third Edition

Book description

“Using this book has propelled my efficiency in solving leetcode challenges.” - Discord user, zizu Python isn't all about object-oriented programming. Discover a valuable way of thinking about code design through a function-first approach – and learn when you need to use it. Now with detailed exercises at the end of every chapter! Purchase of the print or Kindle book includes a free eBook in PDF format.

Key Features

  • Learn how, when, and why to adopt functional elements in your projects
  • Explore the Python modules essential to functional programming, like itertools and functools
  • Cover examples relevant to mathematical, statistical, and data analysis domains

Book Description

Not enough developers understand the benefits of functional programming, or even what it is. Author Steven Lott demystifies the approach, teaching you how to improve the way you code in Python and make gains in memory use and performance. If you’re a leetcoder preparing for coding interviews, this book is for you.

Starting from the fundamentals, this book shows you how to apply functional thinking and techniques in a range of scenarios, with Python 3.10+ examples focused on mathematical and statistical algorithms, data cleaning, and exploratory data analysis. You'll learn how to use generator expressions, list comprehensions, and decorators to your advantage. You don't have to abandon object-oriented design completely, though – you'll also see how Python's native object orientation is used in conjunction with functional programming techniques.

By the end of this book, you'll be well-versed in the essential functional programming features of Python and understand why and when functional thinking helps. You'll also have all the tools you need to pursue any additional functional topics that are not part of the Python language.

What you will learn

  • Use Python's libraries to avoid the complexities of state-changing classes
  • Leverage built-in higher-order functions to avoid rewriting common algorithms
  • Write generator functions to create lazy processing
  • Design and implement decorators for functional composition
  • Make use of Python type annotations to describe parameters and results of functions
  • Apply functional programming to concurrency and web services
  • Explore the PyMonad library for stateful simulations

Who this book is for

The functional paradigm is very useful for programmers working in data science or preparing for technical interviews, but any Python developer who wants to create more reliable, succinct, and expressive code will have much to learn from this book. No prior knowledge of functional programming is required to get started, though Python programming knowledge is assumed. A running Python environment is essential.

Table of contents

  1. Preface
  2. Chapter 1: Understanding Functional Programming
    1. 1.1 The functional style of programming
    2. 1.2 Comparing and contrasting procedural and functional styles
    3. 1.3 A classic example of functional programming
    4. 1.4 Exploratory data analysis
    5. 1.5 Summary
    6. 1.6 Exercises
    7. Join our community Discord space
  3. Chapter 2: Introducing Essential Functional Concepts
    1. 2.1 Functions as first-class objects
    2. 2.2 Immutable data
    3. 2.3 Strict and non-strict evaluation
    4. 2.4 Lazy and eager evaluation
    5. 2.5 Recursion instead of an explicit loop state
    6. 2.6 Functional type systems
    7. 2.7 Familiar territory
    8. 2.8 Learning some advanced concepts
    9. 2.9 Summary
    10. 2.10 Exercises
    11. Join our community Discord space
  4. Chapter 3: Functions, Iterators, and Generators
    1. 3.1 Writing pure functions
    2. 3.2 Functions as first-class objects
    3. 3.3 Using strings
    4. 3.4 Using tuples and named tuples
    5. 3.5 Using generator expressions
    6. 3.6 Cleaning raw data with generator functions
    7. 3.7 Applying generators to built-in collections
    8. 3.8 Summary
    9. 3.9 Exercises
    10. Join our community Discord space
  5. Chapter 4: Working with Collections
    1. 4.1 An overview of function varieties
    2. 4.2 Working with iterables
    3. 4.3 Using any() and all() as reductions
    4. 4.4 Using len() and sum() on collections
    5. 4.5 Using zip() to structure and flatten sequences
    6. 4.6 Using sorted() and reversed() to change the order
    7. 4.7 Using enumerate() to include a sequence number
    8. 4.8 Summary
    9. 4.9 Exercises
    10. Join our community Discord space
  6. Chapter 5: Higher-Order Functions
    1. 5.1 Using max() and min() to find extrema
    2. 5.2 Using the map() function to apply a function to a collection
    3. 5.3 Using the filter() function to pass or reject data
    4. 5.4 The iter() function with a sentinel value
    5. 5.5 Using sorted() to put data in order
    6. 5.6 Overview of writing higher-order functions
    7. 5.7 Writing higher-order mappings and filters
    8. 5.8 Building higher-order functions with callables
    9. 5.9 Review of some design patterns
    10. 5.10 Summary
    11. 5.11 Exercises
    12. Join our community Discord space
  7. Chapter 6: Recursions and Reductions
    1. 6.1 Simple numerical recursions
    2. 6.2 Reductions and folding a collection from many items to one
    3. 6.3 Group-by reduction from many items to fewer
    4. 6.4 Summary
    5. 6.5 Exercises
    6. Join our community Discord space
  8. Chapter 7: Complex Stateless Objects
    1. 7.1 Using tuples to collect data
    2. 7.2 Using NamedTuple to collect data
    3. 7.3 Using frozen dataclasses to collect data
    4. 7.4 Complicated object initialization and property computations
    5. 7.5 Using pyrsistent to collect data
    6. 7.6 Avoiding stateful classes by using families of tuples
    7. 7.7 Polymorphism and type pattern matching
    8. 7.8 Summary
    9. 7.9 Exercises
    10. Join our community Discord space
  9. Chapter 8: The Itertools Module
    1. 8.1 Working with the infinite iterators
    2. 8.2 Using the finite iterators
    3. 8.3 Cloning iterators with tee()
    4. 8.4 The itertools recipes
    5. 8.5 Summary
    6. 8.6 Exercises
    7. Join our community Discord space
  10. Chapter 9: Itertools for Combinatorics – Permutations and Combinations
    1. 9.1 Enumerating the Cartesian product
    2. 9.2 Reducing a product
    3. 9.3 Performance improvements
    4. 9.4 Permuting a collection of values
    5. 9.5 Generating all combinations
    6. 9.6 Recipes
    7. 9.7 Summary
    8. 9.8 Exercises
    9. Join our community Discord space
  11. Chapter 10: The Functools Module
    1. 10.1 Function tools
    2. 10.2 Memoizing previous results with cache
    3. 10.3 Defining classes with total ordering
    4. 10.4 Applying partial arguments with partial()
    5. 10.5 Reducing sets of data with the reduce() function
    6. 10.6 Handling multiple types with singledispatch
    7. 10.7 Summary
    8. 10.8 Exercises
    9. Join our community Discord space
  12. Chapter 11: The Toolz Package
    1. 11.1 The itertools star map function
    2. 11.2 Reducing with operator module functions
    3. 11.3 Using the toolz package
    4. 11.4 Summary
    5. 11.5 Exercises
  13. Chapter 12: Decorator Design Techniques
    1. 12.1 Decorators as higher-order functions
    2. 12.2 Cross-cutting concerns
    3. 12.3 Composite design
    4. 12.4 Adding a parameter to a decorator
    5. 12.5 Implementing more complex decorators
    6. 12.6 Complicated design considerations
    7. 12.7 Summary
    8. 12.8 Exercises
  14. Chapter 13: The PyMonad Library
    1. 13.1 Downloading and installing
    2. 13.2 Functional composition and currying
    3. 13.3 Functors – making everything a function
    4. 13.4 Monad bind() function
    5. 13.5 Implementing simulation with monads
    6. 13.6 Additional PyMonad features
    7. 13.7 Summary
    8. 13.8 Exercises
    9. Join our community Discord space
  15. Chapter 14: The Multiprocessing, Threading, and Concurrent.Futures Modules
    1. 14.1 Functional programming and concurrency
    2. 14.2 What concurrency really means
    3. 14.3 Using multiprocessing pools and tasks
    4. 14.4 Using a multiprocessing pool for concurrent processing
    5. 14.5 Summary
    6. 14.6 Exercises
  16. Chapter 15: A Functional Approach to Web Services
    1. 15.1 The HTTP request-response model
    2. 15.2 The WSGI standard
    3. 15.3 Defining web services as functions
    4. 15.4 Tracking usage
    5. 15.5 Summary
    6. 15.6 Exercises
    7. Join our community Discord space
  17. Other Books You Might Enjoy
  18. Index

Product information

  • Title: Functional Python Programming - Third Edition
  • Author(s): Steven F. Lott
  • Release date: December 2022
  • Publisher(s): Packt Publishing
  • ISBN: 9781803232577