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