zoukankan      html  css  js  c++  java
  • 14-面向过程与函数式

    一、编程思想/范式

    编程范式指的是编程的套路,分为面向对象函数式面向对象等等

    二、面向过程

    2.1 概念

    ​ ”面向过程“核心是“过程”二字,“过程”指的是解决问题的步骤,即先干什么再干什么......,基于面向过程开发程序就好比在设计一条流水线,是一种机械式的思维方式,这正好契合计算机的运行原理:任何程序的执行最终都需要转换成cpu的指令流水按过程调度执行,即无论采用什么语言、无论依据何种编程范式设计出的程序,最终的执行都是过程式的。

    优点:复杂的问题流程化、进而简单化

    缺点:扩展性非常差

    2.2 应用场景

    1、不是所有的软件都需要频繁更迭:比如编写脚本

    2、即便是一个软件需要频繁更迭,也不并不代表这个软件所有的组成部分都需要一起更迭

    三、函数式

    函数式编程并非用函数编程这么简单,而是将计算机的运算视为数学意义上的运算,比起面向过程,函数式更加注重的是执行结果而非执行的过程,代表语言有:Haskell、Erlang。而python并不是一门函数式编程语言,但是仍为我们提供了很多函数式编程好的特性,如lambda,map,reduce,filter

    3.1 匿名函数与lambda

    def---定义有名函数

    # func=函数的内存地址
    def func(x,y):
        return x+y
    
    print(func)

    lambda---定义匿名函数

    res = lambda x,y:x+y          # 相当于上述func
    # x,y 形参,x+y 为返回值

    lambda函数的调用方式

    # 方式一:
    res=(lambda x,y:x+y)(1,2)
    print(res)
    
    # 方式二:
    func=lambda x,y:x+y
    res=func(1,2)
    print(res)

    # 方式三:
    def func():
      return lambda x,y:x+y res=func(1,2) print(res)

    注:匿名函数用于临时调用一次的场景:更多的是将匿名函数与其他函数配合使用

     3.2 匿名函数的应用(max/min/sorted、map/filter/reduce)

    案例:

    salaries={
        'siry':3000,
        'tom':7000,
        'lili':10000,
        'jack':2000
    }

    1)max函数应用:找出薪资最高的那个人

    # ========================max的应用
    # 有名函数
    def func(k):
        return salaries[k]
    
    res=max(salaries,key=func)  # 返回值=func('siry')
    print(res)                  # lili
    
    # 匿名函数:
    res=max(salaries,key=lambda k:salaries[k])
    print(res)
    def max(*args, key=None): # known special case of max
        """
        max(iterable, *[, default=obj, key=func]) -> value
        max(arg1, arg2, *args, *[, key=func]) -> value
       """

    max的使用:max函数可传入一个可迭代对象,若无key=func,则直接比较每次迭代得到的值,返回最大值。

    若有key=func,则可将每次迭代获得的值传入func,并以func的返回值作为比较依据进行比较,最后返回最大的迭代值。

    2)min 函数的应用:找出薪资最低的那个人

    # ========================min的应用
    res=min(salaries,key=lambda k:salaries[k])
    print(res)
    def min(*args, key=None): # known special case of min
        """
        min(iterable, *[, default=obj, key=func]) -> value
        min(arg1, arg2, *args, *[, key=func]) -> value
        """

    min的使用与max类似

    3)sorted 函数的应用:

    # ========================sorted排序
    salaries={
        'siry':3000,
        'tom':7000,
        'lili':10000,
        'jack':2000
    }
    res=sorted(salaries,key=lambda k:salaries[k],reverse=True)
    print(res)
    def sorted(*args, **kwargs): # real signature unknown
        """
        Return a new list containing all items from the iterable in ascending order.
        
        A custom key function can be supplied to customize the sort order, and the
        reverse flag can be set to request the result in descending order.
        """
        pass

    sorted函数使用:返回一个升序排列的列表。传入一个可迭代对象,以key的返回值进行比较,对可迭代对象中所有元素进行排序并返回。

            默认为升序排列,若想要降序,则置reverse=True

    4) map函数的应用:

    # ========================map的应用(了解)
    l=['alex','lxx','wxx','薛贤妻']
    
    res=map(lambda name:name+'_dsb',l)
    print(res) # 生成器
        """
        map(func, *iterables) --> map object
        
        Make an iterator that computes the function using arguments from
        each of the iterables.  Stops when the shortest iterable is exhausted.
        """

    map函数使用:传入一个函数以及可迭代对象,每次将迭代的值传入func函数,得到func的返回值。

           用func返回值创建一个新的迭代器对象作为map的返回值

    5)fileter函数的应用:

    # ========================filter的应用(了解)
    # l=['alex_sb','lxx_sb','wxx','薛贤妻']
    # res=(name for name in l if name.endswith('sb'))
    # print(res)
    
    res=filter(lambda name:name.endswith('sb'),l)
    print(res)
        """
        filter(function or None, iterable) --> filter object
        
        Return an iterator yielding those items of iterable for which function(item)
        is true. If function is None, return the items that are true.
        """

    filter函数的使用:filter(func,iterable),将可迭代对象传入func,返回值若为True则保留,反之则丢弃。最后以迭代器形式作为filter的返回值。

            若func=None,可过滤传入的可迭代对象中值为0,None,空的对象。

    6)reduce函数的应用

    # ========================reduce的应用(了解)
    from functools import reduce
    res=reduce(lambda x,y:x+y,[],10)           # 10
    print(res)
    
    res=reduce(lambda x,y:x+y,[1,2,3],10)      # 16
    print(res)
    
    res=reduce(lambda x,y:x-y,[1,2,3],10)      # 4
    print(res)
    
    res=reduce(lambda x,y:x+y,['a','b','c'])   # "abc"
    print(res)
        """
        reduce(function, sequence[, initial]) -> value
    
        Apply a function of two arguments cumulatively to the items of a sequence,
        from left to right, so as to reduce the sequence to a single value.
        For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates
        ((((1+2)+3)+4)+5).  If initial is present, it is placed before the items
        of the sequence in the calculation, and serves as a default when the
        sequence is empty.
        """

    reduce函数使用:reduce(func,sequence,initial) 当initial存在时,将initial作为第一个参数传入func,将序列中第一个元素作为第二参数传入func,

            得到func的返回值,以返回值作为第一参数传入,序列中下一个元素作为第二个元素传入,以此类推,直至序列结束得到一个值。

            该值就是reduce的返回值。

  • 相关阅读:
    web监听器
    闭包
    函数表达式
    android 反向暴力取私有参数 (转载)
    html/weui slider
    自定义取值范围的EditText(记录)
    Android 基于OpenGL ES2.0 的CircleProgressBar
    Android 二维码扫描
    android 反编译网址记录
    Android Opengl ES & Jni 使用
  • 原文地址:https://www.cnblogs.com/zhubincheng/p/12577625.html
Copyright © 2011-2022 走看看