English
Let's just try again! How can I add all the resources in a group list to 1?
Def addone (list ):
Result = []
For ele in list:
Result. append (Ele + 1)
Return result
How about changing it to the function type?
Def addone (list ):
Return [] If list = [] else [list [0] + 1] + addone (list [1:])
So we need to multiply all the resources in a group list by 3?
Def multiplythree (list ):
Return [] If list = [] else [list [0] * 1] + multiplythree (list [1:])
What if we want to put all the information in a group list in ooxx? The following figure shows a general map:
Def map (func, list ):
Return [] If list = [] else [func (list [0])] + map (func, list [1:])
So you can add 1 to all the resources in a group list:
Map (lambda ele: ele + 1, list)
Multiply all the resources in a group list by 3:
Map (lambda ele: ele * 3, list)
You can set the item ooxx in A group list:
Map (lambda ele: ooxx (Ele), list)
Similarly, how can we extract elements greater than 3 in the list over the limit?
Def greatthanthree (list ):
Result = []
For ele in list:
If ele> 3:
Result. append (Ele)
Return result
How about changing it to the function type?
Def greatthanthree (list ):
Return [] If list = [] else \
([List [0] + greatthanthree (list [1:]) If list [0]> 3 else greatthanthree (list [1:])
What if I want to extract elements smaller than 3 in the list over the limit? You will output something similar to the structure, so you can directly create a filter!
Def filter (func, list ):
Return [] If list = [] else \
([List [0] + filter (func, list [1:]) If func (list [0]) else filter (func, list [1:])
So the elements smaller than 3 in the list should be written out as follows:
Filter (lambda ele: ele> 3, list)
From this perspective, we can see that the fold of map, filter, and previous articles are very common operations, because the program often performs these actions on a group of resources. But have you found that the structure of MAP and filter is similar to fold? In reality, MAP and filter can all be implemented using fold. For example, you can directly use reduce in Python functool to compile a map:
Def map (func, list ):
Return reduce (lambda lt, ELEM: Lt + [func (ELEM)], list, [])
You can also use reduce to verify the filter:
Def filter (func, list ):
Return reduce (lambda lt, ELEM: Lt + [ELEM] If func (ELEM) else lt, list, [])
You can directly add 1 to all the resources in a group list using reduce, but it will convert to reduce (lambda lt, ELEM: Lt + [ELEM + 1], list, []), you can directly use reduce to extract elements smaller than 3 in the list, but instead it can be converted to reduce (lambda lt, ELEM: Lt
+ [ELEM] If ELEM> 3 else lt, list, []), it is better to differentiate it as map (lambda ele: ele + 1, list) clear with filter (lambda ele: ele> 3, list). Of course, you can refer to addone (list), greaterthanthree (list ), this is a matter of atomicity. The general reduce, map, and filter functions are not too popular. You can use map, filter, and reduce to duplicate to the end, however, you may wish to clarify your words. Add a general account [1,
2, 3]. How about sum ([1, 2, 3]) built in Python? Or reduce (lambda S, E: S + e, [1, 2, 3], 0? Or reduce (Int. _ add __, [1, 2, 3], 0?
In python, in addition to reduce, functools also have map and filter, but the reverse return value is not exactly the same as what is actually done here, in python, MAP and filter are not directly returned to the list, but are divided into map and filter objects. In simple terms, they are actually produced.
If you want to obtain the list, you can use the list function. For example, list (MAP (Int. _ add __, [1, 2, 3]) or list (filter (lambda ele: ele> 3, [1, 2, 3, 4, 5]).
In fact, some examples provide the List comprehension statement method, which can be used for map and filter operations. For example, if you want to add 1 to all elements in Python, You Can [ele + 1 for ele in list] to overwrite 3 elements, you can use [ele for ele in list if ele> 3] to familiarize yourself with python.
Basically, map and filter are obtained, and list comprehension is obtained. However, if list comprehension is obtained, MAP and filter may be a bit wordy. For example, when a condition is nested:
[(I, j) for I in [1, 2, 3] For J in [4, 5, 6]
This produces [(1, 4), (1, 5), (1, 6), (2, 4), (2, 5), (2, 6), (3, 4), (3, 5), (3, 6)], if you use the map defined here, add reduce in functools to compute, at least convert:
Reduce (list. _ add __, map (lambda I: Map (lambda J: (I, j), [4, 5, 6]), [1, 2, 3])
However, the list comprehension method is not just set up to allow reading, but will make its child more like a data table in Some acronyms, for example, the number of data columns in a positive integer ranging from 1 to 100 is calculated as {2 * x | x ε {1, 2 .. 100 }}, if you use Haskell's list comprehension to compile, it will be [2
* X | x <-[1, 2 .. 100], which is consistent with the original mathematical definition.