Skip to content

Instantly share code, notes, and snippets.

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

  • Save kl0tl/52622496e8d18e063138 to your computer and use it in GitHub Desktop.

Select an option

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