Java has generics and C++ has templates. These language features are only half of a functor, since they only act on types, not functions. In JavaScript we can do better.
// Stack interface var stackOf = function (t, u) { u = u || t; func(t); func(u); return makeInterface({ push: hom(t, id), // pop returns either [0, []] or [1, value] pop: hom([], makeCoproduct([terminal, u])), size: hom([], nat32) }); }; // Stack implementation var makeStack = function (t, u) { var state = []; return stackOf(t, u)({ push: function (val) { state.push(val); }, pop: function () { return state.length ? [1, state.pop()] : [0, []]; }, size: function () { return state.length; } }; }; // Create a stack of strings. var myStack = makeStack(str);
This definition of stack is a little odd because it has the option of taking two contracts instead of just one. A functor like arrOf
is both a contract and the map
function. If we think of stackOf
as a kind of map
, then what does it do?
In arrOf
, the contract or function gets applied to each element of the array. In stackOf
, one contract or function gets applied before pushing and the other gets applied after popping. Given an associative array obj
and a stackOf(str)
object myStack
, we could write
var myLookupStack = stackOf( id, function (s) { return obj[s]; })(myStack);
and get a stack object where you push strings but pop values of obj
.
A stack homomorphism from an implementation stackOf(t)(x)
to stackOf(u)(y)
is a function f: t -> u
such that stack(f, u)(y)
gives the same result as stack(t, f)(x)
. This notion of a stack homomorphism hasn’t appeared in the literature (at least not under the name “stack homomorphism”) even though it’s very natural from a category theory perspective. I believe Java and C++ developers didn’t come up with it just because generics and templates deal only with types rather than both types and functions between them.