码迷,mamicode.com
首页 > 其他好文 > 详细

2. Building Abstractions with Data

时间:2015-07-18 10:52:16      阅读:122      评论:0      收藏:0      [点我收藏+]

标签:

程序的构造:

将数据抽象(data abstraction), 将操作data的过程用函数抽象(function abstraction)


abstraction barriers

访问数据的某些属性时就用相应的方法,而不是从数据的构造方法入手


sequence processing:

  • sequence iteration
  • list comprehensions
  • aggregation: aggregate all values in a sequence into a single value. The built-in functions sum, min, and max are all examples of aggregation functions.
  • higher-order functions

 

Closure property: a method for combining data values has a closure property if the result of combination can itself be combined using the same method. Closure is the key to power in any means of combination because it permits us to create hierarchical structures — structures made up of parts, which themselves are made up of parts, and so on.


Dispatch function:a general method for implementing a message passing interface for abstract data. The function is a dispatch function and its arguments are first a message, followed by additional arguments to parameterize that method. This message is a string naming what the function should do. Dispatch functions are effectively many functions in one: the message determines the behavior of the function, and the additional arguments are used in that behavior.

 

Generic function:a function that can accept values of multiple different types. We will consider three different techniques for implementing generic functions

  • shared interfaces
  • Type dispatching: Look up a cross-type implementation of an operation based on the types of its arguments
  • Type coercion: Look up a function for converting one type to another, then apply a type-specific implementation.

 

count how many times function calls

def count(f):
    def counted(*args):
        counted.call_count += 1
        return f(*args)
    counted.call_count = 0
    return counted

count how many frames are active

def count_frames(f):
    def counted(*args):
        counted.open_count += 1
        counted.max_count = max(counted.max_count, counted.open_count)
        result = f(*args)
        counted.open_count -= 1
        return result
    counted.open_count = 0
    counted.max_count = 0
    return counted

Memoization(using cache)

def memo(f):
    cache = {}
    def memoized(n):
        if n not in cache:
            cache[n] = f(n)
        return cache[n]
    return memoized

 

Lexical scope:
The parent of a frame is the environment in which a procedure was defined
Dynamic scope:
The parent of a frame is the environment in which a procedure was called

 

tail recursion
一般递归花费时间与迭代相同, 但花费内存远大于迭代
tail recursion 就是不断的将用不到的frame舍弃

pdf中一个非tail context 转化为 tail context
(recursion is the last thing in your procudure are tail context) #也给了我们什么时候用递归 什么时候用迭代方便的思路(所有可以用迭代的都可以写成tail recursion)

 

 

 

 2015-07-18

2. Building Abstractions with Data

标签:

原文地址:http://www.cnblogs.com/whuyt/p/4656334.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!