Skip to content

Instantly share code, notes, and snippets.

@Zemnmez
Last active August 29, 2015 14:02
Show Gist options
  • Save Zemnmez/2b18697b95d4c7842d21 to your computer and use it in GitHub Desktop.
Save Zemnmez/2b18697b95d4c7842d21 to your computer and use it in GitHub Desktop.
Python style lambda functions for Javascript
macro lambda {
rule { $arg:ident: $stm:expr (,) ... } => { function($arg) {
return $stm (,) ...;
}}
rule { $arg:ident (,) ...: $stm:expr (,) ...} => {function(x){
var i = 0, $($arg = x[i++]) (,) ...;
return $stm (,) ...;
}}
}
/*
var y = [1,2,3];
y = y.map(lambda x: x+1);
var z = [[1,2],[2,3], [3,4]];
z = z.map(lambda x, y: x + y);
//becomes
console.log([1, 2, 3].map(function(a) {
return a + 1;
}), [[1, 2], [2, 3], [3, 4]].map(function(a) {
var b = 0, c = a[b++];
a = a[b++];
return c + a;
}));
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment