Created
June 7, 2014 22:02
-
-
Save kl0tl/52622496e8d18e063138 to your computer and use it in GitHub Desktop.
Inline `Array#indexOf`
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_indexOf { | |
| rule { ($list:expr, $needle:expr) } => { | |
| (function (ref, needle) { | |
| var length = ref.length; | |
| for (var i = 0; i < length; i += 1) { | |
| if (ref[i] === needle) return i; | |
| } | |
| return -1; | |
| }($list, $needle)); | |
| } | |
| } | |
| macro indexOf { | |
| rule infix { $list:expr . | () } => { | |
| inline_indexOf($list, undefined) | |
| } | |
| rule infix { $list:expr . | ($needle:expr) } => { | |
| inline_indexOf($list, $needle) | |
| } | |
| } | |
| export indexOf; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment