Python functional programming: higher-order functions, Map/reduce

Source: Internet
Author: User

Python functional programming: higher-order functions, Map/reduce

1 #function-type programming2 #one feature of functional programming is that it allows the function itself to be passed as a parameter to another function, and also allows a function to be returned3 #(i) Higher order functions4 5f=ABS6 F7 Print(F (-20))8 #Results9 #A function can assign a value to a variable, namely: A variable can point to a functionTen #So what is the function name? The function name is actually a variable pointing to the function!  One  A #following the code, after execution, ABS has not pointed to an absolute value function but an integer of 10.  - #after the ABS (-10) will be error, unable to execute, you must restart Python.  - #abs=10 the #ABS ( -10) -  - #a simple high-order function - #The so-called higher order function, that is, the function can be passed as its parameters.  + defAdd (x,y,f): -     returnF (x) +f (Y) +  A Print(Add ( -5,6, ABS)) at  - #demonstrate higher order function map ()/reduce () - #The map () function receives two parameters, one is a function, and the other is a function that iterable,map the passed in sequence. - #acts on each element of the sequence and returns the result as a new iterator - #For example, we have a function f (x) =x^2, to effect it on a list[1,2,3,4,5,6,7,8,9] -  in deff (x): -     returnx*x toR=map (f,[1,2,3,4,5,6,7,8,9]) + #iterator is an iterator, which is an inert sequence that requires the list () function to calculate the entire sequence to return a list - Print(List (r)) the #results [1,4,9,16,25,36,49,64,81] *  $ #map () is a high-order function, in fact it abstracts the arithmetic rules. Panax Notoginseng #One more example, converting all of the list numbers to strings - Print(List (Map (str,[1,2,3,4,5,6,7,8]))) the #results [' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 '] +  A #reduce () function the #The effect of this is: + #reduce (f,[x1,x2,x3,x4]) =f (f (f (x1,x2), x3), x4) -  fromFunctoolsImportReduce $ deffn (x, y): $     returnx*10+y -  - Print(Reduce (fn,[1,3,5,7,9])) the #Result: 13579, which turns the sequence into an integer - Wuyi #Another example of implementing the function of the Int () function the defStr2Int (s): -     deffn (x, y): Wu         returnx*10+y -     defChar2num (s): About         return{'0'70A'1': 1,'2': 2,'3': 3,'4': 4, $                 '5'75A'6': 6,'7': 7,'8': 8,'9': 9}[s] -     returnReduce (Fn,map (char2num,s)) - Print(Str2Int ('784533')) - #result 784533 A  + #You can also use lambda functions to further simplify the the defChar2num (s): -     return{'0': 0,'1': 1,'2': 2,'3': 3,'4': 4, $             '5': 5,'6': 6,'7': 7,'8': 8,'9'8 {}[s] the defStr2Int (s): the     returnReduceLambdax,y:x*10+Y,map (char2num,s)) the Print(Str2Int ('743092')) the #result 743092

Python functional programming: higher-order functions, Map/reduce

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.