Skip to content

Instantly share code, notes, and snippets.

@mytharcher
Created November 29, 2012 06:39
Show Gist options
  • Save mytharcher/4167198 to your computer and use it in GitHub Desktop.
Save mytharcher/4167198 to your computer and use it in GitHub Desktop.
JavaScript snips
function binarySearch (array, matcher) {
var len = array.length,
head = 0,
tail = len - 1,
offset = got = Math.ceil(tail / 2);
if (offset == tail) {
return offset;
}
var m = matcher(array[got]);
if (m) {
if (m < 0) {
return offset + binarySearch(array.slice(offset), matcher);
} else {
return head + binarySearch(array.slice(0, offset), matcher);
}
} else {
return got;
}
}
function compare (referrence, input) {
return input - referrence;
}
function equalTo (refer) {
return function (num) {
return compare(refer, num);
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment