We’ve seen stack homomorphisms and monoid homomorphisms. These are homomorphisms between implementations of an interface.
There’s also a general notion of a homomorphism between two arbitrary functors, which is a variant of the hom functor.
var natural = function (f, g) { func(f); func(g); return function (x, y) { y = y || x; func(x); func(y); return hom([f(x)], g(y)); }; };
Just as we used hom
when defining the interface of a monoid, we can use natural
when defining the interface of a monad:
var compose = function (f, g) { func(f); func(g); return function(x) { return f(g(x)); }; }; var monadOf = function (m, n) { n = n || m; func(m); func(n); return function (intr) { return function (t) { return makeInterface({ '*': natural(compose(m, m), n)(t), 1: natural(id, n)(t) })(intr); }; }; };
Given categories and and functors and , a natural transformation assigns to every object of a morphism such that for every morphism in C,
The flatten function is a natural transformation from compose(arrOf, arrOf)
to arrOf
. It takes a doubly-nested array to a singly-nested array, no matter what the type of the elements in the array. Also, it doesn’t matter if you first map a function over the list of lists and then flatten:
flatten(int)(arrOf(arrOf(function (x) { return x+1; }))([[1, 2], [3]]))
or if you flatten first and then map:
arrOf(function (x) { return x+1; }))(flatten(int)([[1, 2], [3]])
.
Leave a Reply