Functional Python Programming: Use a functional approach to write succinct, expressive, and efficient Python code, 3rd Edition
معرفی کتاب «Functional Python Programming: Use a functional approach to write succinct, expressive, and efficient Python code, 3rd Edition» نوشتهٔ Steven F. Lott, Ricardo Bánffy، منتشرشده توسط نشر Packt Publishing در سال 2022. این کتاب در 5 صفحه، فرمت pdf، زبان انگلیسی ارائه شده است. «Functional Python Programming: Use a functional approach to write succinct, expressive, and efficient Python code, 3rd Edition» در دستهٔ بدون دستهبندی قرار دارد.
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 Revised to cover new features of Python 3.10, exercises at the end of every chapter, and more 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. Starting from the fundamentals, this book shows you how to apply functional thinking and techniques in a range of scenarios, with examples centered around 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, 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 Understanding Functional Programming Introducing Essential Functional Concepts Functions, Iterators, and Generators Working with Collections Higher-Order Functions Recursions and Reductions Complex Stateless Objects The Itertools Module Itertools for Combinatorics - Permutations and Combinations The Functools Module The Toolz Package Decorator Design Techniques The PyMonad Library The Multiprocessing, Threading, and Concurrent.Futures Modules A Functional Approach toWeb Services A Chi-Squared Case Study Cover Copyright Contributors Table of Contents Preface Chapter 1: Understanding Functional Programming The functional style of programming Comparing and contrasting procedural and functional styles Using the functional paradigm Using a functional hybrid The stack of turtles A classic example of functional programming Exploratory data analysis Summary Exercises Convert an imperative algorithm to functional code Convert step-wise computation to functional code Revise the sqrt() function Data cleansing steps (Advanced) Optimize this functional code Chapter 2: Introducing Essential Functional Concepts Functions as first-class objects Pure functions Higher-order functions Immutable data Strict and non-strict evaluation Lazy and eager evaluation Recursion instead of an explicit loop state Functional type systems Familiar territory Learning some advanced concepts Summary Exercises Apply map() to a sequence of values Function vs. lambda design question Optimize a recursion 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 Applying generators to built-in collections Generators for lists, dicts, and sets Using stateful mappings Using the bisect module to create a mapping Using stateful sets Summary Exercises Rewrite the some_function() function Alternative Mersenne class definition Alternative algorithm implementations Map and filter Dictionary comprehension Raw data cleanup 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 iter() function explicitly Extending an iteration Applying generator expressions to scalar functions Using any() and all() as reductions Using len() and sum() on collections 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 sorted() and reversed() to change the order Using enumerate() to include a sequence number Summary Exercises Palindromic numbers Hands of cards Replace legs() with pairwise() 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 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 iter() function with a sentinel value Using sorted() to put data in order Overview of 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 Building higher-order functions with callables Assuring good functional design Review of some design patterns Summary Exercises Classification of state Classification of state, Part II Optimizing a file parser Chapter 6: Recursions and Reductions Simple numerical recursions Implementing manual tail-call optimization Leaving recursion in place Handling difficult tail-call optimization Processing collections through recursion Tail-call optimization for collections Using the assignment (sometimes called the "walrus") operator in recursions Reductions and folding a collection from many items to one Tail-call optimization using deques 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 Exercises Multiple recursion and caching Refactor the all_print() function Parsing CSV files Classification of state, Part III Diesel engine data Chapter 7: Complex Stateless Objects Using tuples to collect data Using NamedTuple to collect data Using frozen dataclasses to collect data Complicated object initialization and property computations Using pyrsistent to collect data Avoiding stateful classes by using families of tuples Computing Spearman's rank-order correlation Polymorphism and type pattern matching Summary Exercises Frozen dictionaries Dictionary-like sequences Revise the rank_xy() function to use native types Revise the rank_corr() function Revise the legs() function to use pyrsistent Chapter 8: The Itertools Module Working with the infinite iterators Counting with count() Counting with float arguments Re-iterating a cycle with cycle() Using cycle() for data sampling Repeating a single value with repeat() Using the finite iterators Assigning numbers with enumerate() Running totals with accumulate() Combining iterators with chain() Partitioning an iterator with groupby() Merging iterables with zip_longest() and zip() Creating pairs with pairwise()) Filtering with compress() Picking subsets with islice() Stateful filtering with dropwhile() and takewhile() Two approaches to filtering with filterfalse() and filter() Applying a function to data via starmap() and map() Cloning iterators with tee() The itertools recipes Summary Exercises Optimize the find_first() function Compare Chapter 4 with the itertools.pairwise() recipe Compare Chapter 4 with itertools.tee() recipe Splitting a dataset for training and testing purposes Rank ordering Chapter 9: Itertools for Combinatorics – Permutations and Combinations Enumerating the Cartesian product Reducing a product Computing distances Getting all pixels and all colors Performance improvements Rearranging the problem Combining two transformations Permuting a collection of values Generating all combinations Combinations with replacement Recipes Summary Exercises Alternative distance computations Actual domain of pixel color values Cribbage hand scoring Chapter 10: The Functools Module Function tools Memoizing previous results with cache Defining classes with total ordering Applying partial arguments with partial() Reducing sets of data with the reduce() function Combining map() and reduce() Using the reduce() and partial() functions Using the map() and reduce() functions to sanitize raw data Using the groupby() and reduce() functions Avoiding problems with reduce() Handling multiple types with singledispatch Summary Exercises Compare string.join() and reduce() Extend the comma_fix() function Revise the clean_sum() function Chapter 11: The Toolz Package The itertools star map function Reducing with operator module functions Using the toolz package Some itertoolz functions Some dicttoolz functions Some functoolz functions Summary Exercises Replace true division with a fraction Color file parsing Anscombe's quartet parsing Waypoint computations Waypoint geofence Callable object for the row_counter() function Chapter 12: Decorator Design Techniques Decorators as higher-order functions Using the functools update_wrapper() function Cross-cutting concerns Composite design Preprocessing bad data Adding a parameter to a decorator Implementing more complex decorators Complicated design considerations Summary Exercises Datetime conversions Optimize a decorator None-tolerant functions Logging Dry-run check Chapter 13: The PyMonad Library Downloading and installing Functional composition and currying Using curried higher-order functions Functional composition with PyMonad Functors – making everything a function Using the lazy ListMonad() monad Monad bind() function Implementing simulation with monads Additional PyMonad features Summary Exercises Revise the arctangent series Statistical computations Data validation Multiple models Chapter 14: The Multiprocessing, Threading, and Concurrent.Futures Modules Functional programming and concurrency What concurrency really means The boundary conditions Sharing resources with process or threads Where benefits will accrue Using multiprocessing pools and tasks Processing many large files Parsing log files – gathering the rows Parsing log lines into named tuples Parsing additional fields of an Access object Filtering the access details Analyzing the access details The complete analysis process Using a multiprocessing pool for concurrent processing Using apply() to make a single request More complex multiprocessing architectures Using the concurrent.futures module Using concurrent.futures thread pools Using the threading and queue modules Using async functions Designing concurrent processing Summary Exercises Lazy parsing Filter access path details Add @cache decorators Create sample data Change the pipeline structure Chapter 15: A Functional Approach to Web Services The HTTP request-response model Injecting state through cookies Considering a server with a functional design Looking more deeply into the functional view Nesting the services The WSGI standard Raising exceptions during WSGI processing Pragmatic web applications Defining web services as functions Flask application processing The data access tier Applying a filter Serializing the results Serializing data with JSON or CSV formats Serializing data with XML and HTML Tracking usage Summary Exercises WSGI application: welcome WSGI application: demo Serializing data with XML Serializing data with HTML Packt page Other Books You Might Enjoy Index “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 FeaturesLearn how, when, and why to adopt functional elements in your projectsExplore the Python modules essential to functional programming, like itertools and functoolsCover examples relevant to mathematical, statistical, and data analysis domainsBook DescriptionNot 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 learnUse Python's libraries to avoid the complexities of state-changing classesLeverage built-in higher-order functions to avoid rewriting common algorithmsWrite generator functions to create lazy processingDesign and implement decorators for functional compositionMake use of Python type annotations to describe parameters and results of functionsApply functional programming to concurrency and web servicesExplore the PyMonad library for stateful simulationsWho this book is forThe 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.
دانلود کتاب Functional Python Programming: Use a functional approach to write succinct, expressive, and efficient Python code, 3rd Edition