News for hackers. Stuff that matters.
Re:Category Theory for Programmers by leoh on Thursday April 30, 2015 @02:36AM (#9463613)
I like the first lesson (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

There are no comments on this comment yet.