Created
June 29, 2014 16:44
-
-
Save kl0tl/abd6046271bb56344505 to your computer and use it in GitHub Desktop.
Inline `Array.from`
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 inline_from { | |
rule { ($constructor:expr, $list:expr, function ($value:ident, $index:ident, $ref:ident) { | |
$return:expr | |
}, $context:expr) } => { | |
(function (constructor, ref) { | |
var length = ref.length, res = new constructor(length); | |
for (var $index = 0; $index < length; $index += 1) { | |
var $value = ref[$index]; | |
res[$index] = $return; | |
} | |
return res; | |
}.call($context, $constructor, $list)); | |
} | |
} | |
let from = macro { | |
rule infix { $symbol:ident (.) ... $constructor:ident . | ($list:expr, () => $return:expr) } => { | |
inline_from($symbol (.) ... $constructor, $list, function (value, index, ref) { $return }, this) | |
} | |
rule infix { $constructor:expr . | ($list:expr, () => $return:expr) } => { | |
inline_from($constructor, $list, function (value, index, ref) { $return }, this) | |
} | |
rule infix { $symbol:ident (.) ... $constructor:ident . | ($list:expr, $value:ident => $return:expr) } => { | |
inline_from($symbol (.) ... $constructor, $list, function ($value, index, ref) { $return }, this) | |
} | |
rule infix { $constructor:expr . | ($list:expr, $value:ident => $return:expr) } => { | |
inline_from($constructor, $list, function ($value, index, ref) { $return }, this) | |
} | |
rule infix { $symbol:ident (.) ... $constructor:ident . | ($list:expr, ($value:ident) => $return:expr) } => { | |
inline_from($symbol (.) ... $constructor, $list, function ($value, index, ref) { $return }, this) | |
} | |
rule infix { $constructor:ident . | ($list:expr, ($value:ident) => $return:expr) } => { | |
inline_from($constructor, $list, function ($value, index, ref) { $return }, this) | |
} | |
rule infix { $symbol:ident (.) ... $constructor:ident . | ($list:expr, ($value:ident, $index:ident) => $return:expr) } => { | |
inline_from($symbol (.) ... $constructor, $list, function ($value, $index, ref) { $return }, this) | |
} | |
rule infix { $constructor:expr . | ($list:expr, ($value:ident, $index:ident) => $return:expr) } => { | |
inline_from($constructor, $list, function ($value, $index, ref) { $return }, this) | |
} | |
rule { $rest ... } => { from $rest ... } | |
} | |
export from; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment