A-M baby
This commit is contained in:
parent
764da6cbbb
commit
db842d013d
19005 changed files with 197040 additions and 7 deletions
15
Task/First-class-functions/Python/first-class-functions.py
Normal file
15
Task/First-class-functions/Python/first-class-functions.py
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
>>> # Some built in functions and their inverses
|
||||
>>> from math import sin, cos, acos, asin
|
||||
>>> # Add a user defined function and its inverse
|
||||
>>> cube = lambda x: x * x * x
|
||||
>>> croot = lambda x: x ** (1/3.0)
|
||||
>>> # First class functions allow run-time creation of functions from functions
|
||||
>>> # return function compose(f,g)(x) == f(g(x))
|
||||
>>> compose = lambda f1, f2: ( lambda x: f1(f2(x)) )
|
||||
>>> # first class functions should be able to be members of collection types
|
||||
>>> funclist = [sin, cos, cube]
|
||||
>>> funclisti = [asin, acos, croot]
|
||||
>>> # Apply functions from lists as easily as integers
|
||||
>>> [compose(inversef, f)(.5) for f, inversef in zip(funclist, funclisti)]
|
||||
[0.5, 0.4999999999999999, 0.5]
|
||||
>>>
|
||||
Loading…
Add table
Add a link
Reference in a new issue