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