Skip to content

Instantly share code, notes, and snippets.

@skeggse
Created February 5, 2014 21:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save skeggse/8833227 to your computer and use it in GitHub Desktop.
Save skeggse/8833227 to your computer and use it in GitHub Desktop.
Binary Search Generator Thingy
function* locate(low, high) {
var mid;
if (high === Infinity || high === undefined) {
mid = 1;
for (;;) {
if ((yield mid) > 0) {
mid <<= 1;
} else {
high = mid << 1;
break;
}
}
}
while (low < high) {
mid = (low + high) >>> 1;
(yield mid) > 0 ? (low = mid + 1) : (high = mid);
}
return low;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment