Skip to content

Instantly share code, notes, and snippets.

@L8D
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save L8D/3a7e0c449313643f447f to your computer and use it in GitHub Desktop.
Save L8D/3a7e0c449313643f447f to your computer and use it in GitHub Desktop.
things
function Tuple(a, b, c, d, e, f, g) {
function tuplable(transformer) {
return transformer(a, b, c, d, e, f, g);
};
tuplable.toString = function() {
return 'Tuple (' + [a, b, c, d, e, f, g].filter(function(item) {
return item != null;
}).join(', ') + ')';
};
return tuplable;
}
var EmptyList = {
toString: function() {
return 'List []';
}
};
function List(head, rest) {
function listable(iterator) {
return iterator(head, rest);
};
listable.toString = function() {
return 'List [' + toJS(this).join(', ') + ']';
};
return listable;
}
function map(iterator, list) {
if (list === EmptyList) {
return list;
} else {
return list(function(head, rest) {
return List(iterator(head), map(iterator, rest));
});
}
}
function filter(qualifier, list) {
if (list === EmptyList) {
return list;
} else {
return list(function(head, rest) {
if (qualifier(head)) {
return List(head, filter(qualifier, rest));
} else {
return filter(qualifier, rest);
}
});
}
}
function reduce(accumulator, value, list) {
if (list === EmptyList) {
return value;
} else {
return list(function(head, rest) {
return reduce(accumulator, accumulator(value, head), rest);
});
}
}
function concat(list, list$) {
if (list === EmptyList) {
return list$;
} else {
return list(function(head, rest) {
return List(head, concat(rest, list$));
});
}
}
function concatl(list) {
return reduce(function(result, list) {
return concat(result, list);
}, EmptyList, list);
}
function flatmap(iterator, list) {
if (list === EmptyList) {
return list;
} else {
return list(function(head, rest) {
return concat(iterator(head), flatmap(iterator, rest));
});
}
}
function fromJS(array) {
return array.reduceRight(function(list, element) {
return List(element, list);
}, EmptyList);
}
function toJS(list) {
return reduce(function(array, element) {
return array.concat([element]);
}, [], list);
};
var Nothing = {
toString: function() {
return 'Nothing';
}
};
function Just(value) {
function justable(iterator) {
return iterator(value);
}
justable.toString = function() {
return 'Just ' + this;
};
return justable;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment