Category Theory for Programmers Chapter 1: Category: The Essence of Composition

Just my notes and solutions for Chapter 1.

  1. Identity function in javascript
    const id = x => x
    
  2. Composition function in javascript. I didn’t want to think too hard since initial input can have multiple arguments, but the second function must have only 1 argument.
    const compose = (f,g) =>
      function() { return g(f.apply(null, arguments)) }
    
  3. This seems good enough for me.
    compose(id, x => x + 2)(2)
    compose(x => x + 2, id)(2)
    
  4. The world-wide web is a category if the objects are webpages and the morphism is “can navigate to” which taken very loosely to mean something like “without changing the URL of the current page A, is there a series of clicks or searches you can perform to eventually navigate topage B”.

    The second part of the question is not a category since a link on A to B and a link on B to C does not mean there exists a link on A to C.

  5. A friend of my friend is not necessarily my friend, so Facebook is not a category.

  6. A directed graph is a category if for any 3 vertices a, b, c in a graph G, there exists edges from a to a, a to b, b to c, and a to c.