This article wrote about forgetting hair. Now fill it up.
Python is not and is unlikely to be a functional programming language, but it supports many valuable functional programming language constructs.
Some of them behave like functional programming mechanisms, but they are not traditionally considered as functional programming languages. Python is provided in the form of a four-in-built function and a lambda expression.
anonymous functions and lambda
Lambda [Arg1, [Arg2, ... argn]]:expression
Python allows anonymous functions to be created using the Lambda keyword. Anonymity is because it doesn't need to be declared in a standard way. However, as functions, they can also have parameters. A complete lambda "statement" represents an expression in which the body of the expression must be placed on the same line as the declaration. Parameters are optional, and parameters are usually part of an expression if used.
The following example shows the similarities between a single-line statement and a lambda statement:
def True (): return true>>> true () TrueLambda : True <function <LambdaLambda : True>>> fun () True
Here are some of the use differences of expressions
def return x + ylambda x, y:x + ydefreturn# with default parameters Lambda x, y=2:x + ydefreturn zLambda *z:z
A lambda expression works like a function that creates a frame object when it is called. After assigning it to an object, the object is the corresponding function.
Built-in functions
Filter ()
After the function is given an object sequence and a filter function, each sequence element is filtered by this filter, preserving the object returned as true.
If we want to write our own filter (), it's probably like this:
def filter (Bool_func, seq):
= [] for in seq: if bool_func (eachitem): Filtered_seq.append (each Item) return filtered_seq
Map ()
The map () built-in function is similar to filter () because it can also handle sequences through functions. However, unlike filter (), map () "Maps" a function call to an element of each sequence and returns a list with all the returned values.
If we want to write our own map (), it's probably like this:
def= [] for in Seq:mapped_seq.append (func (Eachitem)) return
Mapped_seq
Reduce ()
Reduce () uses a two-tuple function, a sequence, and an optional initializer to effectively "reduce" the contents of that list to a single value, as if it were a name. In other languages, this concept is called folding.
It does this by taking the first two elements of the sequence, passing them to a two-tuple function to get a single value, then using the value and the next element of the sequence to get another value, and then until the entire sequence has been traversed and the last value is computed.
If we want to write reduce () ourselves, it's probably like this:
defReduce (Bin_func, seq, init=None): Iseq= List (seq)#Convert to List ifInit isNone:#initializer?res = lseq.pop (0)#No Else: Res= Init#Yes forIteminchLSEQ:#Reduce sequenceres = Bin_func (res, item)#Apply function returnRes#return result
Partial function Application
Partial functions (partial function application, PFA), which convert functions of any number (sequential) arguments to another function object with remaining parameters.
This concept is related to the concept of currying, currying, which refers to the transformation of a function that accepts multiple parameters into a function that accepts a single parameter, and returns a technique that takes the remaining parameters and returns the result of the new function.
This is a bit of an abstraction, and you can assume that this is similar to the case where default parameters are used without arguments. In the PFA example, the parameter does not need to invoke the default value of the function, simply call the collection explicitly.
Here's an example:
from Import Add,mul from Import partial>>> add1 = partial (add,1)>>> add1 (Ten)11>>> mul100 = Partial (Mul, over)>>> mul100 (5)500
This partial function call is boring and does not show the power of the partial function, but this allows us to know how to use it. PFA is the best way to call a function with many parameters.
2015/9/18 Python Basics (14): Functional programming