Functional Python Programming - Second Edition

Book description

Create succinct and expressive implementations with functional programming in Python

About This Book
  • Learn how to choose between imperative and functional approaches based on expressiveness, clarity, and performance
  • Get familiar with complex concepts such as monads, concurrency, and immutability
  • Apply functional Python to common Exploratory Data Analysis (EDA) programming problems
Who This Book Is For

This book is for Python developers who would like to perform Functional programming with Python. Python Programming knowledge is assumed.

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 features to ensure efficient functional programs
  • Use Python strings with object-oriented suffix notation and prefix notation
  • Avoid stateful classes with families of tuples
  • Design and implement decorators to create composite functions
  • Use functions such as max(), min(), map(), filter(), and sorted()
  • Write higher-order functions
In Detail

If you're a Python developer who wants to discover how to take the power of functional programming (FP) and bring it into your own programs, then this book is essential for you, even if you know next to nothing about the paradigm.

Starting with a general overview of functional concepts, you'll explore common functional features such as first-class and higher-order functions, pure functions, and more. You'll see how these are accomplished in Python 3.6 to give you the core foundations you'll build upon. After that, you'll discover common functional optimizations for Python to help your apps reach even higher speeds.

You'll learn FP concepts such as lazy evaluation using Python's generator functions and expressions. Moving forward, you'll learn to design and implement decorators to create composite functions. You'll also explore data preparation techniques and data exploration in depth, and see how the Python standard library fits the functional programming model. Finally, to top off your journey into the world of functional Python, you'll at look at the PyMonad project and some larger examples to put everything into perspective.

Style and approach

This book provides a general overview of functional concepts and then delves deeper into the functional features, showing you how the Python standard library fits the functional programming model. It also demonstrates how to implement common functional programming design patterns and techniques in Python.

Table of contents

  1. Title Page
  2. Copyright and Credits
    1. Functional Python Programming Second Edition
  3. Packt Upsell
    1. Why subscribe?
    2. PacktPub.com
  4. Contributors
    1. About the author
    2. About the reviewer
    3. Packt is searching for authors like you
  5. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
      1. Download the example code files
      2. Conventions used
    4. Get in touch
      1. Reviews
  6. Understanding Functional Programming
    1. Identifying a paradigm
    2. Subdividing the procedural paradigm
      1. Using the functional paradigm
      2. Using a functional hybrid
      3. Looking at object creation
      4. The stack of turtles
    3. A classic example of functional programming
    4. Exploratory data analysis
    5. Summary
  7. Introducing Essential Functional Concepts
    1. First-class functions
      1. Pure functions
      2. Higher-order functions
    2. Immutable data
    3. Strict and non-strict evaluation
    4. Recursion instead of an explicit loop state
    5. Functional type systems
    6. Familiar territory
    7. Learning some advanced concepts
    8. Summary
  8. Functions, Iterators, and Generators
    1. Writing pure functions
    2. Functions as first-class objects
    3. Using strings
    4. Using tuples and named tuples
      1. Using generator expressions
      2. Exploring the limitations of generators
      3. Combining generator expressions
    5. Cleaning raw data with generator functions
    6. Using lists, dicts, and sets
      1. Using stateful mappings
      2. Using the bisect module to create a mapping
      3. Using stateful sets
    7. Summary
  9. Working with Collections
    1. An overview of function varieties
    2. Working with iterables
      1. Parsing an XML file
      2. Parsing a file at a higher level
      3. Pairing up items from a sequence
      4. Using the iter() function explicitly
      5. Extending a simple loop
      6. Applying generator expressions to scalar functions
      7. Using any() and all() as reductions
      8. Using len() and sum()
      9. Using sums and counts for statistics
    3. Using zip() to structure and flatten sequences
      1. Unzipping a zipped sequence
      2. Flattening sequences
      3. Structuring flat sequences
      4. Structuring flat sequences – an alternative approach
    4. Using reversed() to change the order
    5. Using enumerate() to include a sequence number
    6. Summary
  10. Higher-Order Functions
    1. Using max() and min() to find extrema
    2. Using Python lambda forms
    3. Lambdas and the lambda calculus
    4. Using the map() function to apply a function to a collection
      1. Working with lambda forms and map()
    5. Using map() with multiple sequences
    6. Using the filter() function to pass or reject data
    7. Using filter() to identify outliers
    8. The iter() function with a sentinel value
    9. Using sorted() to put data in order
    10. Writing higher-order functions
    11. Writing higher-order mappings and filters
      1. Unwrapping data while mapping
      2. Wrapping additional data while mapping
      3. Flattening data while mapping
      4. Structuring data while filtering
    12. Writing generator functions
    13. Building higher-order functions with callables
      1. Assuring good functional design
    14. Review of some design patterns
    15. Summary
  11. Recursions and Reductions
    1. Simple numerical recursions
      1. Implementing tail-call optimization
      2. Leaving recursion in place
      3. Handling difficult tail-call optimization
      4. Processing collections through recursion
      5. Tail-call optimization for collections
      6. Reductions and folding a collection from many items to one
    2. Group-by reduction from many items to fewer
      1. Building a mapping with Counter
      2. Building a mapping by sorting
      3. Grouping or partitioning data by key values
      4. Writing more general group-by reductions
      5. Writing higher-order reductions
      6. Writing file parsers
        1. Parsing CSV files
        2. Parsing plain text files with headers
    3. Summary
  12. Additional Tuple Techniques
    1. Using tuples to collect data
    2. Using named tuples to collect data
    3. Building named tuples with functional constructors
    4. Avoiding stateful classes by using families of tuples
      1. Assigning statistical ranks
      2. Wrapping instead of state changing
      3. Rewrapping instead of state changing
      4. Computing Spearman rank-order correlation
    5. Polymorphism and type-pattern matching
    6. Summary
  13. The Itertools Module
    1. Working with the infinite iterators
      1. Counting with count()
      2. Counting with float arguments
      3. Re-iterating a cycle with cycle()
      4. Repeating a single value with repeat()
    2. Using the finite iterators
      1. Assigning numbers with enumerate()
      2. Running totals with accumulate()
      3. Combining iterators with chain()
      4. Partitioning an iterator with groupby()
      5. Merging iterables with zip_longest() and zip()
      6. Filtering with compress()
      7. Picking subsets with islice()
      8. Stateful filtering with dropwhile() and takewhile()
      9. Two approaches to filtering with filterfalse() and filter()
      10. Applying a function to data via starmap() and map()
    3. Cloning iterators with tee()
    4. The itertools recipes
    5. Summary
  14. More Itertools Techniques
    1. Enumerating the Cartesian product
    2. Reducing a product
      1. Computing distances
      2. Getting all pixels and all colors
      3. Performance analysis
      4. Rearranging the problem
      5. Combining two transformations
    3. Permuting a collection of values
    4. Generating all combinations
    5. Recipes
    6. Summary
  15. The Functools Module
    1. Function tools
    2. Memoizing previous results with lru_cache
    3. Defining classes with total ordering
      1. Defining number classes
    4. Applying partial arguments with partial()
    5. Reducing sets of data with the reduce() function
      1. Combining map() and reduce()
      2. Using the reduce() and partial() functions
      3. Using the map() and reduce() functions to sanitize raw data
      4. Using the groupby() and reduce() functions
    6. Summary
  16. Decorator Design Techniques
    1. Decorators as higher-order functions
      1. Using the functools update_wrapper() functions
    2. Cross-cutting concerns
    3. Composite design
      1. Preprocessing bad data
    4. Adding a parameter to a decorator
    5. Implementing more complex decorators
    6. Complex design considerations
    7. Summary
  17. The Multiprocessing and Threading Modules
    1. Functional programming and concurrency
    2. What concurrency really means
      1. The boundary conditions
      2. Sharing resources with process or threads
      3. Where benefits will accrue
    3. Using multiprocessing pools and tasks
      1. Processing many large files
      2. Parsing log files – gathering the rows
      3. Parsing log lines into namedtuples
      4. Parsing additional fields of an Access object
      5. Filtering the access details
      6. Analyzing the access details
      7. The complete analysis process
    4. Using a multiprocessing pool for concurrent processing
      1. Using apply() to make a single request
      2. Using the map_async(), starmap_async(), and apply_async() functions
      3. More complex multiprocessing architectures
      4. Using the concurrent.futures module
      5. Using concurrent.futures thread pools
      6. Using the threading and queue modules
      7. Designing concurrent processing
    5. Summary
  18. Conditional Expressions and the Operator Module
    1. Evaluating conditional expressions
      1. Exploiting non-strict dictionary rules
      2. Filtering true conditional expressions
      3. Finding a matching pattern
    2. Using the operator module instead of lambdas
      1. Getting named attributes when using higher-order functions
    3. Starmapping with operators
    4. Reducing with operator module functions
    5. Summary
  19. The PyMonad Library
    1. Downloading and installing
    2. Functional composition and currying
      1. Using curried higher-order functions
      2. Currying the hard way
    3. Functional composition and the PyMonad * operator
    4. Functors and applicative functors
      1. Using the lazy List() functor
    5. Monad bind() function and the >> operator
    6. Implementing simulation with monads
    7. Additional PyMonad features
    8. Summary
  20. A Functional Approach to Web Services
    1. The HTTP request-response model
      1. Injecting state through cookies
      2. Considering a server with a functional design
      3. Looking more deeply into the functional view
      4. Nesting the services
    2. The WSGI standard
      1. Throwing exceptions during WSGI processing
      2. Pragmatic WSGI applications
    3. Defining web services as functions
      1. Creating the WSGI application
      2. Getting raw data
      3. Applying a filter
      4. Serializing the results
      5. Serializing data into JSON or CSV formats
      6. Serializing data into XML
      7. Serializing data into HTML
    4. Tracking usage
    5. Summary
  21. Optimizations and Improvements
    1. Memoization and caching
    2. Specializing memoization
    3. Tail recursion optimizations
    4. Optimizing storage
    5. Optimizing accuracy
      1. Reducing accuracy based on audience requirements
    6. Case study–making a chi-squared decision
      1. Filtering and reducing the raw data with a Counter object
      2. Reading summarized data
      3. Computing sums with a Counter object
      4. Computing probabilities from Counter objects
    7. Computing expected values and displaying a contingency table
      1. Computing the chi-squared value
      2. Computing the chi-squared threshold
      3. Computing the incomplete gamma function
      4. Computing the complete gamma function
      5. Computing the odds of a distribution being random
    8. Functional programming design patterns
    9. Summary
  22. Other Books You May Enjoy
    1. Leave a review - let other readers know what you think

Product information

  • Title: Functional Python Programming - Second Edition
  • Author(s): Steven F. Lott
  • Release date: April 2018
  • Publisher(s): Packt Publishing
  • ISBN: 9781788627061