Skip to content

Instantly share code, notes, and snippets.

@bttmly
bttmly / curry-es6.js
Last active August 29, 2015 14:27
Curry is a one-liner in idiomatic es6
let curry = (fn, ...args) => args.length >= fn.length ? fn(...args) : (...more) => curry(fn, ...args, ...more)
// just to make the gist longer.
[..., last] = arr
var Bluebird = require("bluebird");
var nba = require("nba");
Object.keys(nba.api).forEach(function (method) {
var original = nba.api[method];
nba.api[method] = Promise.promisify(original);
});
@bttmly
bttmly / prototypal.js
Last active August 29, 2015 14:12
instanceof w prototypal instantiation
var helloProto = {
toString: function () {
return "Hello!";
}
}
function helloFactory () {
return Object.create(helloProto);
}
/**
* Enable route to __noSuchMethod__ when unknown method calling.
*
* @param {Object} obj Target object.
* @return {Object}
*/
function enableMethodMissing(obj) {
var functionHandler = createBaseHandler({});
functionHandler.get = function(receiver, name) {
@bttmly
bttmly / poser-push.js
Created October 11, 2014 17:25
poser push benchmark
function grabIt (thing) {
var frame = document.createElement('iframe');
document.body.appendChild(frame);
var thing = frame.contentWindow[thing];
document.body.removeChild(frame);
return thing;
}
var Collection = grabIt("Array");
@bttmly
bttmly / keys-bench.js
Last active August 29, 2015 14:07
own keys benchmarking
var Benchmark = require( 'benchmark' );
var len = process.argv[2] || 3;
var obj = {};
for ( var i = 0; i < len; i++ ) {
obj[i] = "value" + i;
}
@bttmly
bttmly / singleton.js
Created August 28, 2014 22:41
Singleton
var Singleton = (function() {
var instance;
return function () {
if ( !this instanceof Singleton ) {
return new Singleton();
}
if ( !instance ) {
instance = this;
}
return instance;
function applyConstructor ( ctor, args ) {
var instance;
function C () {}
C.prototype = ctor.prototype;
var instance = new C();
ctor.apply( instance, args );
return instance;
}
@bttmly
bttmly / slide1.md
Created August 21, 2014 02:07
Iteration presentation

Why use Array.prototype iterators?

  • Expressive They say something about the purpose and result of the iteration.

  • Contained They don't have side effects if used properly. All relevant stuff occurs within the callback.

  • Scoped They provide automatic function scope for each iteration which eliminates a common type of error.