September Morn Update
This commit is contained in:
parent
4e2d22a71d
commit
aac6731f2c
6856 changed files with 141342 additions and 21127 deletions
43
Task/Function-composition/Python/function-composition-4.py
Normal file
43
Task/Function-composition/Python/function-composition-4.py
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
from functools import reduce
|
||||
from numbers import Number
|
||||
import math
|
||||
|
||||
|
||||
def main():
|
||||
'''Test'''
|
||||
|
||||
f = composeList([
|
||||
lambda x: x / 2,
|
||||
succ,
|
||||
math.sqrt
|
||||
])
|
||||
|
||||
print(
|
||||
f(5)
|
||||
)
|
||||
|
||||
|
||||
# GENERIC FUNCTIONS ---------------------------------------
|
||||
|
||||
|
||||
# composeList :: [(a -> a)] -> (a -> a)
|
||||
def composeList(fs):
|
||||
'''Composition, from right to left,
|
||||
of a series of functions.'''
|
||||
return lambda x: reduce(
|
||||
lambda a, f: f(a),
|
||||
fs[::-1],
|
||||
x
|
||||
)
|
||||
|
||||
|
||||
# succ :: Enum a => a -> a
|
||||
def succ(x):
|
||||
'''The successor of a value. For numeric types, (1 +).'''
|
||||
return 1 + x if isinstance(x, Number) else (
|
||||
chr(1 + ord(x))
|
||||
)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
Loading…
Add table
Add a link
Reference in a new issue