-
-
Save badboy/78dd154a9d65e592c1b5 to your computer and use it in GitHub Desktop.
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
// sweet.js arrow function macro | |
// https://github.com/mozilla/sweet.js/ | |
macro => { | |
rule infix { ( $arg:ident (,) ... ) | { $body ... $last:expr } } => { | |
(function ( $arg (,) ... ) { | |
$( $body) ... | |
return $last | |
}).bind(this) | |
} | |
rule infix { ( $arg:ident (,) ... ) | $last:expr } => { | |
(function ( $arg (,) ... ) { | |
return $last | |
}).bind(this) | |
} | |
rule infix { () | { $body ... $last:expr } } => { | |
(function ( ) { | |
$body ...; | |
return $last | |
}).bind(this) | |
} | |
rule infix { $x | { $body ... $last:expr } } => { | |
(function ($x) { | |
$body ...; | |
return $last | |
}).bind(this) | |
} | |
rule infix { $x | $last:expr } => { | |
(function ($x) { | |
return $last | |
}).bind(this) | |
} | |
} | |
// From the docs, looks much easier, untested. | |
// macro (=>) { | |
// rule infix { $param:ident | $body:expr } => { | |
// function ($param) { return $body } | |
// } | |
// } | |
var addOne = x => x + 1; | |
var square = x => x * x; | |
console.log(addOne(41)); | |
console.log(square(23)); | |
console.log([1,2,3].map(x => x + 5)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment