Skip to content

Instantly share code, notes, and snippets.

@kl0tl
Created June 7, 2014 22:03
Show Gist options
  • Select an option

  • Save kl0tl/89ca71b2cc94c4b464a2 to your computer and use it in GitHub Desktop.

Select an option

Save kl0tl/89ca71b2cc94c4b464a2 to your computer and use it in GitHub Desktop.
Inline `Array#lastIndexOf`
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