- Functional Python Programming
- Steven F. Lott
- 327字
- 2021-08-27 19:20:24
Summary
In this chapter, we've identified a number of features that characterize the functional programming paradigm. We started with first-class and higher-order functions. The idea is that a function can be an argument to a function or the result of a function. When functions become the object of additional programming, we can write some extremely flexible and generic algorithms.
The idea of immutable data is sometimes odd in an imperative and object-oriented programming language such as Python. When we start to focus on functional programming, however, we see a number of ways that state changes can be confusing or unhelpful. Using immutable objects can be a helpful simplification.
Python focuses on strict evaluation: all sub-expressions are evaluated from left-to-right through the statement. Python, however, does perform some non-strict evaluation. The or, and, and if-else logical operators are non-strict: all subexpressions are not necessarily evaluated. Similarly, a generator function is also non-strict. We can also call this eager vs. lazy. Python is generally eager, but we can leverage generator functions to create lazy evaluation.
While functional programming relies on recursion instead of the explicit loop state, Python imposes some limitations here. Because of the stack limitation and the lack of an optimizing compiler, we're forced to manually optimize recursive functions. We'll return to this topic in Chapter 6, Recursions and Reductions.
Although many functional languages have sophisticated type systems, we'll rely on Python's dynamic type resolution. In some cases, this means we'll have to write manual coercion for various types. It might also mean that we'll have to create class definitions to handle very complex situations. For the most part, however, Python's built-in rules will work very elegantly.
In the next chapter, we'll look at the core concepts of pure functions and how these fit in with Python's built-in data structures. Given this foundation, we can look at the higher-order functions available in Python and how we can define our own higher-order functions.
- Rust編程:入門(mén)、實(shí)戰(zhàn)與進(jìn)階
- vSphere High Performance Cookbook
- arc42 by Example
- INSTANT Passbook App Development for iOS How-to
- Mastering Android Development with Kotlin
- Getting Started with Python and Raspberry Pi
- Building Apple Watch Projects
- Android應(yīng)用開(kāi)發(fā)攻略
- Hands-On Artificial Intelligence with Unreal Engine
- 程序員的算法趣題2
- 編程真好玩:從零開(kāi)始學(xué)網(wǎng)頁(yè)設(shè)計(jì)及3D編程
- CorelDRAW X6中文版應(yīng)用教程(第二版)
- 計(jì)算機(jī)教學(xué)研究與實(shí)踐:2017學(xué)術(shù)年會(huì)論文集
- KVM Virtualization Cookbook
- Unreal Engine 4 Game Development Quick Start Guide