Skip to content

Instantly share code, notes, and snippets.

@afshin
afshin / partial-application-post-4.js
Created March 9, 2012 14:59
Partial Application in JavaScript Revisited 4
(function () {
var has = 'hasOwnProperty', slice = Array.prototype.slice;
Function.prototype.partial = function () {
var method = this, orig = arguments, orig_len = orig.length, key,
new_method = function () {
var arguments_len = arguments.length, args = [], i = 0, j = 0;
if (!arguments_len) return method.apply(this, slice.call(orig));
for (; i < orig_len; i++)
args.push(orig[i] === void 0 ? arguments[j++] : orig[i]);
return method.apply(this, args.concat(slice.call(arguments, j, arguments_len)));
@afshin
afshin / gist:1652897
Created January 21, 2012 14:16
"single statement" … quescol (nested ternaries) binary search
Array.prototype.search = function (x, l, h, m) {
//returns an index >= 0 if arr.search(x) is found
//returns a negative index if arr.search(x) is not found
//if arr.search(x) < 0, Math.floor(-1 * arr.search(x)) is how many items in arr are smaller than x
return arguments.length < 4 ? this.search(x, 0, this.length - 1, Math.floor((this.length - 1) / 2))
: h < l ? l === 0 ? -0.1 : -1 * l
: this[m] < x ? this.search(x, m + 1, h, m + 1 + Math.floor((h - (m + 1)) / 2))
: this[m] > x ? this.search(x, l, m - 1, l + Math.floor((m - 1 - l) / 2))
: m;
};