Last active
August 29, 2015 14:02
-
-
Save kl0tl/4b007e1b695db8fc2edc to your computer and use it in GitHub Desktop.
Inline `Array#forEach`
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
macro statement { | |
rule { return $expression:expr } => { $expression; continue } | |
rule { $expression:expr ... } | |
} | |
macro inline_forEach { | |
rule { ($list:expr, function ($value:ident, $index:ident, $ref:ident) { | |
$body:statement ... | |
}, $context:expr) } => { | |
(function ($ref) { | |
var length = $ref.length; | |
for (var $index = 0; $index < length; $index += 1) { | |
var $value = $ref[$index]; | |
$body ... | |
} | |
}.call($context, $list)); | |
} | |
} | |
let forEach = macro { | |
rule infix { $list:expr . | (() => $body:expr) } => { | |
inline_forEach($list, function (value, index, ref) { $body }, this) | |
} | |
rule infix { $list:expr . | (() => $body:expr, $context:expr) } => { | |
inline_forEach($list, function (value, index, ref) { $body }, this) | |
} | |
rule infix { $list:expr . | (function () { $body ... }) } => { | |
inline_forEach($list, function (value, index, ref) { $body ... }, undefined) | |
} | |
rule infix { $list:expr . | (function () { $body ... }, $context:expr) } => { | |
inline_forEach($list, function (value, index, ref) { $body ... }, $context) | |
} | |
rule infix { $list:expr . | (($value:ident) => $body:expr) } => { | |
inline_forEach($list, function ($value, index, ref) { $body }, this) | |
} | |
rule infix { $list:expr . | (($value:ident) => $body:expr, $context:expr) } => { | |
inline_forEach($list, function ($value, index, ref) { $body }, this) | |
} | |
rule infix { $list:expr . | ($value:ident => $body:expr) } => { | |
inline_forEach($list, function ($value, index, ref) { $body }, this) | |
} | |
rule infix { $list:expr . | ($value:ident => $body:expr, $context:expr) } => { | |
inline_forEach($list, function ($value, index, ref) { $body }, this) | |
} | |
rule infix { $list:expr . | (function ($value:ident) { $body ... }) } => { | |
inline_forEach($list, function ($value, index, ref) { $body ... }, undefined) | |
} | |
rule infix { $list:expr . | (function ($value:ident) { $body ... }, $context:expr) } => { | |
inline_forEach($list, function ($value, index, ref) { $body ... }, $context) | |
} | |
rule infix { $list:expr . | (($value:ident, $index:ident) => $body:expr) } => { | |
inline_forEach($list, function ($value, $index, ref) { $body }, this) | |
} | |
rule infix { $list:expr . | (($value:ident, $index:ident) => $body:expr, $context:expr) } => { | |
inline_forEach($list, function ($value, $index, ref) { $body }, this) | |
} | |
rule infix { $list:expr . | (function ($value:ident, $index:ident) { $body ... }) } => { | |
inline_forEach($list, function ($value, $index, ref) { $body ... }, undefined) | |
} | |
rule infix { $list:expr . | (function ($value:ident, $index:ident) { $body ... }, $context:expr) } => { | |
inline_forEach($list, function ($value, $index, ref) { $body ... }, $context) | |
} | |
rule infix { $list:expr . | (($value:ident, $index:ident, $ref:ident) => $body:expr) } => { | |
inline_forEach($list, function ($value, $index, $ref) { return $body }, this) | |
} | |
rule infix { $list:expr . | (($value:ident, $index:ident, $ref:ident) => $body:expr, $context:expr) } => { | |
inline_forEach($list, function ($value, $index, $ref) { return $body }, this) | |
} | |
rule infix { $list:expr . | (function ($value:ident, $index:ident, $ref:ident) { $body ... }) } => { | |
inline_forEach($list, function ($value, $index, $ref) { $body ... }, undefined) | |
} | |
rule infix { $list:expr . | (function ($value:ident, $index:ident, $ref:ident) { $body ... }, $context:expr) } => { | |
inline_forEach($list, function ($value, $index, $ref) { $body ... }, $context) | |
} | |
rule { $rest ... } => { forEach $rest ... } | |
} | |
export forEach; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment