JavaScript’s objects (in the OOP sense) are all associative arrays; the Array class just has a magical length property that updates when you assign to indices that satisfy the nat32 contract. From a category theory point of view, they’re also products. From a programmer’s point of view, though, they give a nice way of writing an interface.

var obj = function (o) {
  if (typeof o !== 'object' || o === null) {
    throw new TypeError(
        'Expected an object.');
  return o;

var makeInterface = function (intr) {
  for (var i in intr) {
  return function (x) {
    var result = Object.create(
    for (var i in intr) {
      result[i] = intr[i](x[i]);
    return result;

var person = makeInterface({
  name: str,
  age: nat32,
  print: hom(makeProduct([str, nat32]), str)

var joe = person({
  name: 'Joe Brown',
  age: 35,
  print: function (name, age) {
    return name + ' is ' + age + 'year' +
        ((age === 1) ? '' : 's') + ' old';

Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: