Skip to content

Instantly share code, notes, and snippets.

@gravataLonga
Forked from rustedgrail/functionSet.js
Created March 17, 2014 10:02
Show Gist options
  • Save gravataLonga/9596779 to your computer and use it in GitHub Desktop.
Save gravataLonga/9596779 to your computer and use it in GitHub Desktop.
function emptySet(x) {
return false;
}
function insert(elem, set) {
return function(x) {
if (x === elem) { // insert smarter comparison here if needed
return true;
}
return function() { return contains(x, set); };
};
}
function contains(elem, set) {
var retVal = set(elem);
while(typeof retVal === 'function') {
retVal = retVal(elem);
}
return retVal;
}
var aList = insert(3, insert(2, insert(1, emptySet)));
console.log("FIRST: " + contains(2, aList));
console.log("SECOND: " + contains(4, aList));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment