Last active
August 29, 2015 14:07
-
-
Save matthieubulte/f2b6a4f345655b12fd1e to your computer and use it in GitHub Desktop.
purely functional table
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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