- Functional Python Programming
- Steven F. Lott
- 462字
- 2021-08-27 19:20:20
Identifying a paradigm
It's difficult to be definitive on the universe of programming paradigms. For our purposes, we will distinguish between only two of the many paradigms: functional programming and imperative programming. One important distinguishing feature between these two is the concept of state.
In an imperative language, such as Python, the state of the computation is reflected by the values of the variables in the various namespaces; some kinds of statements make a well-defined change to the state by adding or changing (or even removing) a variable. A language is imperative because each statement is a command, which changes the state in some way.
Our general focus is on the assignment statement and how it changes the state. Python has other statements, such as global or nonlocal, which modify the rules for variables in a particular namespace. Statements such as def, class, and import change the processing context. Other statements such as try, except, if, elif, and else act as guards to modify how a collection of statements will change the computation's state. Statements such as for and while, similarly, wrap a block of statements so that the statements can make repeated changes to the state of the computation. The focus of all these various statement types, however, is on changing the state of the variables.
Ideally, each assignment statement advances the state of the computation from an initial condition toward the desired final outcome. This advancing the computation assertion can be challenging to prove. One approach is to define the final state, identify a statement that will establish this final state, and then deduce the precondition required for this final statement to work. This design process can be iterated until an acceptable initial state is derived.
In a functional language, we replace the state—the changing values of variables—with a simpler notion of evaluating functions. Each function evaluation creates a new object or objects from existing objects. Since a functional program is a composition of functions, we can design lower-level functions that are easy to understand, and then design higher-level compositions that can also be easier to visualize than a complex sequence of statements.
Function evaluation more closely parallels mathematical formalisms. Because of this, we can often use simple algebra to design an algorithm, which clearly handles the edge cases and boundary conditions. This makes us more confident that the functions work. It also makes it easy to locate test cases for formal unit testing.
It's important to note that functional programs tend to be relatively succinct, expressive, and efficient compared to imperative (object-oriented or procedural) programs. The benefit isn't automatic; it requires a careful design. This design effort for functional programming is often easier than for procedural programming.
- HornetQ Messaging Developer’s Guide
- 移動UI設計(微課版)
- ThinkPHP 5實戰(zhàn)
- Java高并發(fā)核心編程(卷2):多線程、鎖、JMM、JUC、高并發(fā)設計模式
- Ext JS Data-driven Application Design
- Python高級編程
- concrete5 Cookbook
- C語言程序設計
- Terraform:多云、混合云環(huán)境下實現(xiàn)基礎設施即代碼(第2版)
- OpenResty完全開發(fā)指南:構建百萬級別并發(fā)的Web應用
- Python編程:從入門到實踐(第3版)
- Java圖像處理:基于OpenCV與JVM
- 新印象:解構UI界面設計
- Building Dynamics CRM 2015 Dashboards with Power BI
- Mockito Essentials