Functional Python Programming

Book description

Create succinct and expressive implementations with functional programming in Python

In Detail

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.

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

Table of contents

  1. Functional Python Programming
    1. Table of Contents
    2. Functional Python Programming
    3. Credits
    4. About the Author
    5. About the Reviewers
    6. www.PacktPub.com
      1. Support files, eBooks, discount offers, and more
        1. Why subscribe?
        2. Free access for Packt account holders
    7. Preface
      1. What this book covers
      2. What you need for this book
      3. Who this book is for
      4. Conventions
      5. Reader feedback
      6. Customer support
        1. Downloading the example code
        2. Errata
        3. Piracy
        4. Questions
    8. 1. Introducing 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
    9. 2. Introducing Some Functional Features
      1. First-class functions
        1. Pure functions
        2. Higher-order functions
      2. Immutable data
      3. Strict and non-strict evaluation
      4. Recursion instead of a explicit loop state
      5. Functional type systems
      6. Familiar territory
      7. Saving some advanced concepts
      8. Summary
    10. 3. Functions, Iterators, and Generators
      1. Writing pure functions
      2. Functions as first-class objects
      3. Using strings
      4. Using tuples and namedtuples
        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
    11. 4. 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
    12. 5. 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. Looking at some of the design patterns
      15. Summary
    13. 6. 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 via recursion
        5. Tail-call optimization for collections
        6. Reductions and folding – from many to one
      2. Group-by reductions – from many 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
    14. 7. Additional Tuple Techniques
      1. Using an immutable namedtuple as a record
      2. Building namedtuples with functional constructors
      3. 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 the Spearman rank-order correlation
      4. Polymorphism and Pythonic pattern matching
      5. Summary
    15. 8. The Itertools Module
      1. Working with the infinite iterators
        1. Counting with count()
        2. Reiterating a cycle with cycle()
        3. 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
    16. 9. 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
    17. 10. 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 reduce()
        1. Combining map() and reduce()
        2. Using reduce() and partial()
        3. Using map() and reduce() to sanitize raw data
        4. Using groupby() and reduce()
      6. Summary
    18. 11. Decorator Design Techniques
      1. Decorators as higher-order functions
        1. Using functool's 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 descriptors
      6. Recognizing design limitations
      7. Summary
    19. 12. The Multiprocessing and Threading Modules
      1. What concurrency really means
      2. The boundary conditions
      3. Sharing resources with process or threads
      4. Where benefits will accrue
      5. 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
      6. Using a multiprocessing pool for concurrent processing
        1. Using apply() to make a single request
        2. Using map_async(), starmap_async(), and apply_async()
        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
      7. Summary
    20. 13. Conditional Expressions and the Operator Module
      1. Evaluating conditional expressions
        1. Exploiting non-strict dictionary rules
        2. Filtering true conditional expressions
      2. Using the operator module instead of lambdas
        1. Getting named attributes when using higher-order functions
      3. Starmapping with operators
      4. Reducing with operators
      5. Summary
    21. 14. 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 multiplication operator
      4. Functors and applicative functors
        1. Using the lazy List() functor
      5. Monad concepts, the bind() function and the Binary Right Shift operator
      6. Implementing simulation with monads
      7. Additional PyMonad features
      8. Summary
    22. 15. A Functional Approach to Web Services
      1. The HTTP request-response model
        1. Injecting a state via 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 the JSON or CSV format
        6. Serializing data into XML
        7. Serializing data into HTML
      4. Tracking usage
      5. Summary
    23. 16. Optimizations and Improvements
      1. Memoization and caching
      2. Specializing memoization
        1. Tail recursion optimizations
      3. Optimizing storage
      4. Optimizing accuracy
        1. Reducing accuracy based on audience requirements
      5. Case study – making a chi-squared decision
        1. Filtering and reducing the raw data with a Counter object
        2. Reading summarized data
        3. Computing probabilities from a Counter object
        4. Alternative summary approaches
      6. Computing expected values and displaying a contingency table
        1. Computing the chi-squared value
        2. Computing the chi-squared threshold
        3. Computing the partial gamma value
        4. Computing the complete gamma value
        5. Computing the odds of a distribution being random
      7. Summary
    24. Index

Product information

  • Title: Functional Python Programming
  • Author(s): Steven Lott
  • Release date: January 2015
  • Publisher(s): Packt Publishing
  • ISBN: 9781784396992