4 ms·
I like the first lesson (http://bartoszmilewski.com/2014/11/04/category-the-essence-of-composition/ http://bartoszmilewski.com/2014/11/04/category-the-essence-o
by leoh 12y ago
I like the first lesson (http://bartoszmilewski.com/2014/11/04/category-the-essence-of-composition/ http://bartoszmilewski.com/2014/11/04/category-the-essence-o...). There are some neat challenge problems. It turns out Python does an excellent job of dealing with them:
> Implement, as best as you can, the identity function in your favorite language (or the second favorite, if your favorite language happens to be Haskell).
def identity(x): return x
> Implement the composition function in your favorite language. It takes two functions as arguments and returns a function that is their composition.
def compose(f, g): return lambda *args, **kwargs: f(g(*args, **kwargs))
Testing these out:
>>> compose(lambda x: x+1, lambda y: y-1)(0)
0
>>> compose(identity, identity)(0)
0