栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

python函数式编程

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

python函数式编程

决定梳理这个内容是因为一个课程,

  • 课程ppt:https://github.com/minitorch/slides
  • 使用的时候,下载下来,把index.html文件拖到浏览器,然后方向键下键,ppt以网页形式播放)
1. python函数式编程

大致意思就是,让函数也成为可以传递的参数,是python的一种编程思想。

下面直接举例子说明:

def map(fn):
    """
    Higher-order map. 高阶函数
    See ``_

    Args:
        fn (one-arg function): Function from one value to one value.

    Returns:
        function : A function that takes a list, applies `fn` to each element, and returns a
        new list
    """
    # TODO: Implement for Task 0.3.
    def _map(ls):
        return [fn(x) for x in ls]
    return _map

def neg(x):
	return -x

def negList(ls):
    "Use :func:`map` and :func:`neg` to negate each element in `ls`"
    return map(neg)(ls)

可以看到,

  • 在上面的例子中,map函数本身只有一个参数,就是传入一个函数,
  • 但是在map函数中定义了另一个函数,其有一个参数用来传递数据,同时该函数中进行了数据处理的工作,最终返回的是一个函数。
  • 调用时,map中传入一个参数,为函数名称,同时由于返回对象是一个函数,所以后面的括号(ls)实际上就是传入给返回的函数的参数。

类似的例子如:
zip函数

def zipWith(fn):
    """
    Higher-order zipwith (or map2).

    .. image:: figs/Ops/ziplist.png

    See ``_

    Args:
        fn (two-arg function): combine two values

    Returns:
        function : takes two equally sized lists `ls1` and `ls2`, produce a new list by
        applying fn(x, y) on each pair of elements.

    """
    # TODO: Implement for Task 0.3.
    def _zip(ls1, ls2):
        # return [fn(x,y) for x,y in zip(ls1,ls2)]
        res = []
        for i in range(len(ls1)):
            res.append(fn(ls1[i], ls2[i]))
        return res

    return _zip

def addLists(ls1, ls2):
    "Add the elements of `ls1` and `ls2` using :func:`zipWith` and :func:`add`"
    return zipWith(add)(ls1, ls2)

reduce函数

def reduce(fn, start):
    r"""
    Higher-order reduce.

    .. image:: figs/Ops/reducelist.png


    Args:
        fn (two-arg function): combine two values
        start (float): start value :math:`x_0`

    Returns:
        function : function that takes a list `ls` of elements
        :math:`x_1 ldots x_n` and computes the reduction :math:`fn(x_3, fn(x_2,
        fn(x_1, x_0)))`
    """
    # TODO: Implement for Task 0.3.
    def _reduce(ls, start=start):
        if not ls:
            return start
        start = fn(start, ls[0])
        return _reduce(ls[1:], start)

    return _reduce


def sum(ls):
    "Sum up a list using :func:`reduce` and :func:`add`."
    # TODO: Implement for Task 0.3.
    return reduce(add, 0.0)(ls)

参考:

  • 【Python杂烩】通过python学习函数式编程
  • python中5个常用的内置高阶函数
  • Python进阶:函数式编程(高阶函数,map,reduce,filter,sorted,返回函数,匿名函数,偏函数)…啊啊啊
  • Python 函数式编程
2. 其他(minitorch)

minitorch/slides-master/docs/slides+build2/module0.1.html#/28

函数式编程:

  1. 函数也可以作为参数传递,像其他对象一样使用的一种编程风格
  2. Python支持的几种编程风格之一
  3. 格式化编程的一种良好范式

minitorch/slides-master/docs/slides+build2/module0.1.html#/29

函数式编程,使用函数作为参数,如上所示


minitorch/slides-master/docs/slides+build2/module0.1.html#/30


minitorch/slides-master/docs/slides+build2/module0.1.html#/31

Higher-order Filter(高阶函数/过滤器)


minitorch/slides-master/docs/slides+build2/module0.1.html#/35/0/2

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/581285.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号