Last active
August 29, 2015 14:02
-
-
Save Zemnmez/2b18697b95d4c7842d21 to your computer and use it in GitHub Desktop.
Python style lambda functions for Javascript
This file contains 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
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