📂
CS-NoteBook
  • Introduction
  • CS144
    • concise introduction to Internet
      • 1.1 Networked Applications
      • 1.2 The 4 Layer Internet
      • 1.3 IP
      • 1.4 A Day in the Life of a Packet
      • 1.5 Principle: Packet Switching
      • 1.6 Principle:Layering
      • 1.7 Principle: Encapsulation
      • 1.8 Byte order and packet formats
      • 1.9 name and addresses:IPv4
      • 1.10 Longest Prefix Match for Link Layer
      • 1.11 Address Resolution Protocol(ARP)
      • 1.12 Summary
    • Transport Layer
      • 2.1 The TCP Service Model
      • 2.2 UDP service model
      • 2.3 ICMP(Internet Control Message Protocol 互联网报文控制协议)
      • 2.4 The End-to-End Principle
      • 2.5 Error Detection:3 schemes (Checksum,CRC and MAC)
      • 2.6 Finite State Machines(有限状态机)
      • 2.7 Flow Control
      • 2.8 Sliding window
      • 2.9 Retransmission Strategies
      • 2.10 TCP Header
      • 2.11 TCP Setup and Teardown
      • 2.12 Recap
    • Package Switching
      • 3.1 The history of Internet
      • 3.2 What is packet switching
      • [3.3 End-to-end delay and Queueing delay
      • 3.4 Playback Buffer(回放缓存区)
  • CS 61C
    • 1.4 C Memory Mangement, Usage
    • 1.5 Intro to Assembly Language, MIPS Intro
    • 1.5 extra bits operation
  • CS 61B
  • CS 61A
    • Function
    • Names
    • The Art of the Function
    • Control
    • Higher-Order Function
    • Recursive Function
    • List
    • Non-Local Assignment
    • Iterators
    • Objects
    • Data Abstraction
    • OOP
    • Inheritance
    • Representations
    • Decomposition
    • Scheme
    • Exceptions
    • Calculator
    • Interpreters
    • Declarative_Programming
    • Table
    • Aggregation
      • More_recursion
    • Databases
    • Distributed_Data
    • Tail Recursion
    • Exercises
      • lab00
      • lab01
      • hw01
      • tree Recursion example -- give Change
  • The Web DevelopMent Bootcamp
    • html5
    • css
    • bootstrap3
    • bootstrap4
    • javascript expression
    • javascript function
Powered by GitBook
On this page
  • 高阶函数
  • 作为参数的函数
  • 作为返回值的函数
  • Lambda 表达式
  • Iteration
  • if
  • 逻辑操作
  • Design && Function Examples
  • 函数装饰器

Was this helpful?

  1. CS 61A

Higher-Order Function

高阶函数

  • 接受其他函数作为参数的函数

  • 或者将函数作为返回值的函数

作为参数的函数

  • 作为一般方法的函数

    • 更强大的抽象类型:

      • 一些函数表达了计算的一般方法,独立于它们调用的特定函数。

      • 命名和函数允许我们抽象而远离大量的复杂性

      • 每个通用的概念或方程都能映射为自己的小型函数

    • 缺陷

      • 全局帧会被小型函数弄乱

        • 解决方法: 嵌套函数

作为返回值的函数

def make_adder(n):  # 用来定义这个 function
    """
    >>> add_three = make_adder(3)
    >>> add_three(4)
    7
    """
    def adder(k):  # 将要返回的 function , parent 不是 global [parent = f1] 
        return k+n

    return adder  # 返回的是 function

make_adder(4)(3) # 7
  • 注意 parent 的指向

  • n 在 f2 找没找到,在 f1 找找到了

  • f1 一直存在

Lambda 表达式

  • 使用 Lambda 表达式凭空创建函数,它会求值为匿名函数

  • 函数体具有单个返回表达式的函数

square = lambda x: x * x # lambda x: x * x 求出了一个 function
square(4) # 16
(lambda x: x * x)(3) # 9


>>> def compose1(f,g):
        return lambda x: f(g(x))

#      lambda            x            :          f(g(x))
# "A function that    takes x    and returns     f(g(x))"

# Lambda expressions can be used as an operator
# or operand
negate = lambda f, x: -f(x)
negate(lambda x: x * x, 3)  # -9
  • lambda 表达式做什么?

  • 我们可以把所有的函数写成lambda表达式吗?

  • 在什么情况下 lambda 表达式是有用的?

    • lambda 表达式创建函数。

    • 当对一个 lambda 表达式求值时,它会生成一个函数。

    • 对于那些我们不需要用很久的函数,我们经常使用 lambda 来创建简短的匿名函数。

Iteration

  • function 可以自己调用自己

def print_all(x):
    print(x)
    return print_all

print_all(1)(3)(5)   # 1 3 5

def print_sum(x):
    print(x)
    def sum(y):
        return print_sum(x+y)
    return sum

print_sum(1)(3)(5)   # 1 4 9

if

为什么 if 不是右边的结构?

  • 因为调用的求值规则

逻辑操作

  • 短路

    • A or B

      • A 真 返回 A

        • 1 or 2 # 1

      • A 假 返回 B

        • 0 or 2 # 2

    • A and B

      • A 真 返回 B

        • 1 and 2 # 2

      • A 假 返回 A

        • 0 and 2 # 0

  • 条件语句

    • <consequent> if <predicate> else <alternative>

Design && Function Examples

  • 抽象

    • 函数不区分内部函数和用户自定义函数

  • 变量命名的原则

函数装饰器

  • 将高阶函数用作执行def语句的一部分,叫做装饰器

def trace1(fn):   # 接受函数 f
    def wrapped(x):
        print('-> ', fn, '(', x, ')')    # 打印地址
        return fn(x)  # 返回 f(x)
    return wrapped    # 返回 f(x)

def triple(x):
    return 3 * x

triple # tripe 方法 <function triple at 0x000001458C2EEB80/>

trace1(triple)(4)
# ->  <function triple at 0x000001458C2EEB80> ( 4 )
# 12

@trace1   # 装饰器
def another_triple(x):
    return 3 * x

another_triple # trace1 中的返回函数的方法 <function trace1.<locals>.wrapped at 0x000001458C2EED30>


another_triple(4)
# ->  <function another_triple at 0x000001458C2EEA60> ( 4 )
# 12
  • @trace1 影响 def 的执行规则

    • triple 并没有绑定到这个函数上

    • 而是绑定到了在新定义的函数 triple 上调用 trace1 的返回函数值上。

等价关系:

@trace1   # 装饰器
def another_triple(x):
    return 3 * x

# ==>

def another_triple(x):
    return 3 * x
another_triple = trace1(another_triple)
PreviousControlNextRecursive Function

Last updated 4 years ago

Was this helpful?

returnFunctionExpressionTree
environmentDiagramFroNestedDefStatements
ifStatements