Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
|
|
@ -0,0 +1 @@
|
|||
compose = lambda f, g: lambda x: f( g(x) )
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
>>> compose = lambda f, g: lambda x: f( g(x) )
|
||||
>>> from math import sin, asin
|
||||
>>> sin_asin = compose(sin, asin)
|
||||
>>> sin_asin(0.5)
|
||||
0.5
|
||||
>>>
|
||||
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()
|
||||
40
Task/Function-composition/Python/function-composition-4.py
Normal file
40
Task/Function-composition/Python/function-composition-4.py
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
from functools import reduce
|
||||
from math import sqrt
|
||||
|
||||
|
||||
def compose(*fs):
|
||||
'''Composition, from right to left,
|
||||
of an arbitrary number of functions.
|
||||
'''
|
||||
def go(f, g):
|
||||
return lambda x: f(g(x))
|
||||
|
||||
return reduce(go, fs, lambda x: x)
|
||||
|
||||
|
||||
# ------------------------- TEST -------------------------
|
||||
def main():
|
||||
'''Composition of three functions.'''
|
||||
|
||||
f = compose(
|
||||
half,
|
||||
succ,
|
||||
sqrt
|
||||
)
|
||||
|
||||
print(
|
||||
f(5)
|
||||
)
|
||||
|
||||
|
||||
# ----------------------- GENERAL ------------------------
|
||||
def half(n):
|
||||
return n / 2
|
||||
|
||||
|
||||
def succ(n):
|
||||
return 1 + n
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
23
Task/Function-composition/Python/function-composition-5.py
Normal file
23
Task/Function-composition/Python/function-composition-5.py
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
# Contents of `pip install compositions'
|
||||
|
||||
class Compose(object):
|
||||
def __init__(self, func):
|
||||
self.func = func
|
||||
|
||||
def __call__(self, x):
|
||||
return self.func(x)
|
||||
|
||||
def __mul__(self, neighbour):
|
||||
return Compose(lambda x: self.func(neighbour.func(x)))
|
||||
|
||||
# from composition.composition import Compose
|
||||
if __name__ == "__main__":
|
||||
# Syntax 1
|
||||
@Compose
|
||||
def f(x):
|
||||
return x
|
||||
|
||||
# Syntax 2
|
||||
g = Compose(lambda x: x)
|
||||
|
||||
print((f * g)(2))
|
||||
Loading…
Add table
Add a link
Reference in a new issue