In python, how to use functools to implement functional programming

Get into the habit of writing together! This is the 14th day of my participation in the "Nuggets Daily New Plan·April Update Challenge", click to view the details of the event .

I have shared functional programming with you before. I first felt that functional programming was cool. When chatting with friends and colleagues, did you talk about functional programming? Functional programming was really popular at that time, mainly because of the borrowing of js. With nodejs and npm already on the top3, and firmly in the top3. Because functions in js are first-class citizens, they are inherently friendly to functional programming, oh, let's go back to python, because we love js, and we usually digress when we talk about it.

partial function

Before starting to introduce partial functions and how to implement partial functions in python, let's look at a simple function, this add function is used to add two numbers, python is a weakly typed language, but it also has its own type system, If the type is clear, you can give the type after the parameter, which is estimated to save the interpreter some type inference work.

import functools

def add(a:float, b:float)->float:
    return a + b
复制代码

Well defined such a function, we further think, in functional programming, we hope that the input and output are single. Functional programming mainly extracts the constant, that is, the deterministic part of the program, so as to reduce the complexity of the program. What is the partial function? What is the effect of the partial function on the above.

A simple understanding of a partial function is a secondary encapsulation of the original function, which is to pre-bind some of the parameters of the existing function as specified values ​​to obtain a new function, which is called a partial function.

Currying to implement partial functions

Partial functions can then be implemented by currying, not only in JavaScript but also in other programming languages. Currying is a transformation of a function, which means converting a function from a callable f(a, b, c) to a callable f(a)(b)(c). That is to say, let our add function change from accepting two parameters at the same time to accepting two parameters add(2)(3) in a certain order.

def make_add(a:float)->Callable[[float],float]:
    def add_inner(b:float)->float:
        return a + b

    return add_inner

add_2 = make_add(2)
复制代码

However, we are not satisfied and can be further optimized. Here we define a function. Instead of explicitly defining a function, we can define a lambda function and return it.

def make_add(a:float)->Callable[[float],float]:
    return lambda b: add(a,b)

add_2 = make_add(2)
复制代码

Implement partial function based on functools

If you use the partial method provided by the functools library to implement a partial function, this can make the implementation of the partial function simpler. This function partial receives the first parameter as the name of the function to be wrapped, and the next parameter is the parameter of the original function. The value is specified, and we explain it through the code

add_2 = functools.partial(add,2)
复制代码

We can also pre-assign both parameters at the same time. The following implicitly pre-specifies values ​​for both parameters of the add function, of course, values ​​can also be specified explicitly.

add_2_3 = functools.partial(add,2,3)
复制代码

A partial function can also be obtained by pre-assigning b

add_3 = functools.partial(add,b=3)
复制代码

It may also make you feel a little unnatural, that is, we can also re-change the pre-assigned b when calling. In fact, it is sometimes more flexible and less constraints, which may be prone to problems.

>>> add_3(2,b=5)
7
复制代码

Guess you like

Origin juejin.im/post/7086332127117049887