Function composition: Difference between revisions
→{{header|Python}}: pylint for Python 3, tidying
(→{{header|Python}}: pylint for Python 3, tidying) |
|||
Line 2,002:
Or, expanding slightly:
{{Works with|Python|3}}
<lang python>from math import (acos, cos, asin, sin)
def compose(g, f):▼
'''Right to left function composition.'''
# main :: IO ()
def main():
lambda f: f(0.5),▼
zipWith(compose)▼
([sin, cos, lambda x: x ** 3.0])▼
([asin, acos, lambda x: x ** (1 / 3.0)])▼
)▼
print(list(map(
▲ lambda f: f(0.5),
# GENERIC FUNCTIONS ------------------------------▼
▲ zipWith(compose)(
▲ )([asin, acos, lambda x: x ** (1 / 3.0)])
▲ )))
▲# GENERIC FUNCTIONS ---------------------------------------
▲# compose (>>>) :: (a -> b) -> (b -> c) -> a -> c
▲def compose(f):
▲ return lambda (g): lambda x: f(g(x))
# zipWith :: (a -> b -> c) -> [a] -> [b] -> [c]
def zipWith(f):
'''A list constructed by zipping with a
custom
default tuple constructor.'''
return lambda xs: lambda ys: (
map(f, xs, ys)
)
if __name__ == '__main__':
{{Out}}
<pre>[0.49999999999999994, 0.5000000000000001, 0.5000000000000001]</pre>
|