Skip to content

Instantly share code, notes, and snippets.

@dypsilon
Created August 7, 2016 14:12
Show Gist options
  • Select an option

  • Save dypsilon/c42ba58940579f03ead2c7cc7b6922ca to your computer and use it in GitHub Desktop.

Select an option

Save dypsilon/c42ba58940579f03ead2c7cc7b6922ca to your computer and use it in GitHub Desktop.
This simple test proves, that standard JavaScript arrays are algebraic functors according to the fantasy land spec (https://github.com/fantasyland/fantasy-land)
const eq = (test, a1, a2) => {
if(a1.length === a2.length && a1.every((v,i) => v === a2[i])) {
return;
}
throw new Error(`Test failed: ${ test }`);
};
{ // Functor
const u = Array.of([1, 2, 3]);
const f = (x) => x + 1;
const g = (x) => x + 2;
eq('Functor: identity', u.map(x => x), u);
eq('Functor: composition', u.map(x => f(g(x))), u.map(g).map(f));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment