Skip to content

Instantly share code, notes, and snippets.

@UserGalileo
Created April 14, 2020 17:15
Show Gist options
  • Select an option

  • Save UserGalileo/6ad64ea8449d72cf091fb931457538d1 to your computer and use it in GitHub Desktop.

Select an option

Save UserGalileo/6ad64ea8449d72cf091fb931457538d1 to your computer and use it in GitHub Desktop.
Functions under composition form a Monoid
// These are our elements
const f = x => x + 1;
const g = x => x + 2;
const h = x => x + 3;
// This is our neutral element, a function which does nothing
const id = x => x;
// This is the composition
f(g(h(1))) // 7
// Which is associative by definition...
compose(f, compose(g,h)) === compose(compose(f,g),h)
// If we compose a function with "id", it's like calling the function directly
compose(f, id) === f
compose(id, f) === f
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment