Created
June 5, 2011 21:54
-
-
Save adrusi/1009473 to your computer and use it in GitHub Desktop.
Spec for an extension to javascript that makes it purely functional (with the exception of global variables)
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
define a = b, // global.a = b; | |
c = d; // global.c = d; | |
define f(x) { // global.f = function(x) { | |
// return; | |
} // }; global.f.name = "f"; | |
/* only globals can be reassigned, and only with the `define` keyword */ | |
let (a = b, c = d) { // (function() { var a = b, c = d; (function(a, c) { | |
console.log(a, c); // return console.log(a, c); | |
} // }(a, c)); }()); | |
[ // [ | |
1, 2, 3, // 1, 2, 3, | |
4, 5, 6 // 4, 5, 6 | |
] // ] | |
[ // { | |
a = b, // a: b, | |
c = d // c: d, | |
] // } | |
/* for consistancy with other definitions, and array literal syntax, since objects are more similar to arrays than functions */ | |
var x = y; // #<ERROR: illegal variable declaration> | |
let (a = b, c = d) { // (function() { var a = b, c = d; (function(a, c) { | |
a = 5; // #<ERROR: illegal assignment> | |
} // | |
let (fn = { // (function() { var fn = function() { | |
console.log("x"); // return console.log("x"); | |
}) { // }; return (function(fn) { | |
fn(); // return fn(); | |
} // }(fn)); }()); | |
`+5` // (function($implicit) { return $implicit + 5; }) | |
[`*2` | [1, 2, 3, 4, 5, 6]; `>2`] // $comprehend((function($implicit) { return $implicit * 2; }), [1, 2, 3, 4, 5, 6], (function($implicit) { return $implicit > 2; })) | |
if (1 == 1) { // (function() { if (1 === 1) { | |
console.log("1 = 1"); // return console.log("1 = 1"); | |
} // } | |
else { // else { | |
console.log("awww"); // return console.log("awww"); | |
} // } }()); | |
console.log(cond { // console.log((function() { | |
(1 == 1) { // if (1 === 1) { | |
"yay"; // return "yay"; | |
} // } | |
(1 == 2) { // else if (1 === 2) { | |
"shoot"; // return "shoot"; | |
} // } | |
else { // else { | |
"wtf"; // return "wtf"; | |
} // } | |
}); // }())); | |
switch (x) { // (function() { switch (x) { | |
case (1, 2) { // case 1: case 2: | |
"1 or 2"; // return "1 or 2"; | |
} // break; | |
case (3) { // case 3: | |
"3"; // return "3"; | |
} // break; | |
else { // default: | |
"something else"; // return "something else"; | |
} // } | |
} // }()); | |
for (var i = 0; i < arr.length; i++) { // #<ERROR: illegal loop> | |
console.log(arr[i]); // | |
} // | |
for (i <- arr) { // (function() { var $_ret = []; for (var i = 0; i < arr.length; i++) { $_ret.push((function() { | |
console.log(arr[i]); // return console.log(arr[i]); | |
} // }())); } return $_ret; }()); | |
for (i = val <- arr) { // (function() { var $_ret = []; for (var i = 0; i < arr.length; i++) { var val = arr[i]; $_ret.push((function() { | |
console.log(val); // return console.log(val); | |
} // }())); } return $_ret; }()); | |
for (key <= obj) { // (function() { var $_ret = []; for (var key in obj) { $_ret.push((function() { | |
console.log(obj[key]); // return console.log(obj[key]); | |
} // }())); } return $_ret; }()); | |
for (key = val <= arr) { // (function() { var $_ret = []; for (var key in arr) { var val = obj[key]; $_ret.push((function() { | |
console.log(val); // return console.log(val); | |
} // }())); } return $_ret; }()); | |
/* all the loop constructs above are just syntactic sugar for recursion, which is what should be used for more complex loops */ | |
/* it is intentional that none of these work with `break` or `continue`. That shoul be done with recursion. */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment