Created
November 29, 2012 06:39
-
-
Save mytharcher/4167198 to your computer and use it in GitHub Desktop.
JavaScript snips
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
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