Skip to content

Instantly share code, notes, and snippets.

@badboy
Forked from btd/es6-arrows.sjs
Last active August 29, 2015 14:05
Show Gist options
  • Save badboy/78dd154a9d65e592c1b5 to your computer and use it in GitHub Desktop.
Save badboy/78dd154a9d65e592c1b5 to your computer and use it in GitHub Desktop.
// 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