What is functional programming?
Functional programming (functional programming) is a programming paradigm, that is, how to write a program's methodology. At present, we are familiar with imperative programming, where the imperative programming language refers to all the values of the modified variable as the most basic way of computing the language, functional programming language means the output of a program is defined as the input of the mathematical function of the language, Purely functional programming does not have the concept of an internal state, nor does it have side effects.
Features of functional programming
1. Function is Object
Functions can be assigned directly to variables
2. Have attributes and methods
>>> my_sum =sum>>> sum ([a])6>>> My_sum ([a])6>>>dir (my_sum) ['__call__','__class__','__cmp__','__delattr__','__doc__','__eq__','__format__','__ge__','__getattribute__','__gt__','__hash__','__init__','__le__','__lt__','__module__','__name__','__ne__','__new__','__reduce__','__reduce_ex__','__repr__','__self__','__setattr__','__sizeof__','__str__','__subclasshook__']>>> My_sum.__name__'sum'
3, function can do parameters
def F (b): ... return A +def F1 ( func,a,b): ... return >>> F1 (f,1,3)4
The difference between functional programming and imperative programming
Objective: To implement F (a,b,c,d) = (A+B) using functional programming and imperative programming, respectively *c-d
Command-type programming:
def F1 (a,b,c,d): = A + b = e * c = f- d return g# command-programming implementation ret = f1 (a,b,c,d)
The function is implemented:
def F_add (A, b ): return A + bdef F_sub (b): return A-def F _multi (b): return A * b# functional Programming Implementation RET = F_sub (F_multi (F_add (A , b), c), D)
Python-Functional programming