读书笔记:composing programs - 1.6 Higher-Order Functions

·  阅读 138

functions are a method of abstraction that describe compound operations independent of the particular values of their arguments

Functions that manipulate functions are called higher-order functions

1.6.1 Functions as Arguments

1.6.2 Functions as General Methods

We introduced user-defined functions as a mechanism for abstracting patterns of numerical operations so as to make them independent of the particular numbers involved.

With higher-order functions, we begin to see a more powerful kind of abstraction: some functions express general methods of computation, independent of the particular functions they call.

1. naming and functions allow us to abstract away a vast amount of complexity. While each function definition has been trivial, the computational process set in motion by our evaluation procedure is quite intricate. 
2. it is only by virtue of the fact that we have an extremely general evaluation procedure for the Python language that small components can be composed into complex processes

1.6.3 Defining Functions III: Nested Definitions

Lexical scope:This discipline of sharing names among nested definitions. 
Locally defined functions also have access to the name bindings in the scope in which they are defined. Critically, the inner functions have access to the names in the environment where they are defined (not where they are called)


1. Each user-defined function has a parent environment: the environment in which it was defined.
2. When a user-defined function is called, its local frame extends its parent environment.

Extended Environments. An environment can consist of an arbitrarily long chain of frames, which always concludes with the global frame. 

two key advantages of lexical scoping in Python.
1. The names of a local function do not interfere with names external to the function in which it is defined, because the local function name will be bound in the current local environment in which it was defined, rather than the global environment.
2. A local function can access the environment of the enclosing function, because the body of the local function is evaluated in an environment that extends the evaluation environment in which it was defined.

1.6.4 Functions as Returned Values

An important feature of lexically scoped programming languages is that locally defined functions maintain their parent environment when they are returned.

1.6.5 Example: Newton's Method

1.6.6 Currying

We can use higher-order functions to convert a function that takes multiple arguments into a chain of functions that each take a single argument. More specifically, given a function f(x, y), we can define a function g such that g(x)(y) is equivalent to f(x, y). Here, g is a higher-order function that takes in a single argument x and returns another function that takes in a single argument y. This transformation is called currying.

1.6.7 Lambda Expressions

1.6.8 Abstractions and First-Class Functions

Some of the "rights and privileges" of first-class elements are:

1. They may be bound to names.
2. They may be passed as arguments to functions.
3. They may be returned as the results of functions.
4. They may be included in data structures.

1.6.9 Function Decorators

decorator: apply higher-order functions as part of executing a def statement

>>> def trace(fn):
    def wrapped(x):
        print('-> ', fn, '(', x, ')')
        return fn(x)
    return wrapped

>>> @trace
    def triple(x):
        return 3 * x
分类:
阅读
标签:
分类:
阅读
标签:
收藏成功!
已添加到「」, 点击更改