Skip to content

Instantly share code, notes, and snippets.

@matthieubulte
Last active August 29, 2015 14:07
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 matthieubulte/f2b6a4f345655b12fd1e to your computer and use it in GitHub Desktop.
Save matthieubulte/f2b6a4f345655b12fd1e to your computer and use it in GitHub Desktop.
purely functional table
// found this perl in "The Seasoned Schemer".
// just love how simple this implementation while coming
// with really great features.
function empty_table() {}
function extend(table, key, value) {
return function(k) {
if(k === key) {
return value;
}
else {
return table(k);
}
}
}
var x = empty_table;
var y;
x = extend(x, '1', 'one');
x = extend(x, '2', 'two');
y = extend(x, '3', 'trois');
x = extend(x, '3', 'three');
console.log(x('1')); // one
console.log(x('2')); // two
console.log(y('1')); // one
console.log(y('2')); // two
console.log(x('3')); // three
console.log(y('3')); // trois
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment