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)
 
 
# compose (>>><<<) :: (ab -> bc) -> (ba -> cb) -> a -> c
def compose(g, f):
'''Right to left function composition.'''
return lambda (g): lambda x: g(f(g(x))
 
 
# main :: IO ()
def main():
return map('''Test'''
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)(
( [sin, cos, lambda x: x ** 3.0])
)([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
return (
custom lambdafunction, xs:rather lambdathan ys:with the
default tuple constructor.'''
[f(a)(b) for (a, b) in zip(xs, ys)]
return lambda xs: lambda ys: (
map(f, xs, ys)
)
 
 
if __name__ == '__main__':
print main()</lang>
{{Out}}
<pre>[0.49999999999999994, 0.5000000000000001, 0.5000000000000001]</pre>
9,655

edits