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