September Morn Update
This commit is contained in:
parent
4e2d22a71d
commit
aac6731f2c
6856 changed files with 141342 additions and 21127 deletions
36
Task/Function-composition/Python/function-composition-3.py
Normal file
36
Task/Function-composition/Python/function-composition-3.py
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
from math import (acos, cos, asin, sin)
|
||||
|
||||
|
||||
# compose (<<<) :: (b -> c) -> (a -> b) -> a -> c
|
||||
def compose(g, f):
|
||||
'''Right to left function composition.'''
|
||||
return lambda x: g(f(x))
|
||||
|
||||
|
||||
# main :: IO ()
|
||||
def main():
|
||||
'''Test'''
|
||||
|
||||
print(list(map(
|
||||
lambda f: f(0.5),
|
||||
zipWith(compose)(
|
||||
[sin, cos, lambda x: x ** 3.0]
|
||||
)([asin, acos, lambda x: x ** (1 / 3.0)])
|
||||
)))
|
||||
|
||||
|
||||
# GENERIC FUNCTIONS ---------------------------------------
|
||||
|
||||
|
||||
# zipWith :: (a -> b -> c) -> [a] -> [b] -> [c]
|
||||
def zipWith(f):
|
||||
'''A list constructed by zipping with a
|
||||
custom function, rather than with the
|
||||
default tuple constructor.'''
|
||||
return lambda xs: lambda ys: (
|
||||
map(f, xs, ys)
|
||||
)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
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