Skip to content

Instantly share code, notes, and snippets.

@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;
};
@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 / partial-application-post-1.js
Created March 13, 2012 17:27
Partial Application in JavaScript Revisited 1
Function.prototype.partial = function () {
var fn = this, args = Array.prototype.slice.call(arguments);
return function () {
var arg = 0;
for (var i = 0; i < args.length && arg < arguments.length; i++)
if (args[i] === undefined)
args[i] = arguments[arg++];
return fn.apply(this, args);
};
};
@afshin
afshin / partial-application-post-2.js
Created March 13, 2012 17:40
Partial Application in JavaScript Revisited 2
(function () {
var delay = setTimeout.partial(undefined, 10);
delay(function () {
console.log('A call to this function will be temporarily delayed.');
});
// logs 'A call to this function will be temporarily delayed.'
})();
@afshin
afshin / partial-application-post-3.js
Created March 13, 2012 17:50
Partial Application in JavaScript Revisited 3
(function () {
var delay = setTimeout.partial(undefined, 10);
delay(function () {
console.log('A call to this function will be temporarily delayed.');
});
// logs 'A call to this function will be temporarily delayed.'
delay(function () {console.log('Some other message.');});
// logs 'A call to this function will be temporarily delayed.'
})();
@afshin
afshin / partial-application-post-5.js
Created March 13, 2012 18:12
Partial Application in JavaScript Revisited 5
(function () {
var delay = setTimeout.partial(undefined, 10);
delay(function () {
console.log('A call to this function will be temporarily delayed.');
});
// logs 'A call to this function will be temporarily delayed.'
delay(function () {console.log('Some other message.');});
// logs 'Some other message.'
})();

Keybase proof

I hereby claim:

  • I am afshin on github.
  • I am darian (https://keybase.io/darian) on keybase.
  • I have a public key whose fingerprint is 62C6 231F F6A4 31F8 57B9 29B6 4B0E 7F36 237A CAFC

To claim this, I am signing this object:

@afshin
afshin / menu.ts
Last active January 26, 2016 14:36
menus
/**
* Defines a section within a menu
*/
interface IMenuSection {
id: string;
items: IMenuItem[];
}
/**
* Base for menu items, not to be used by itself.
@afshin
afshin / main.go
Last active April 11, 2016 06:49
gyre leave test
package main
import (
"flag"
"fmt"
"github.com/zeromq/gyre"
"os"
"time"
)
This is just a gist.