Skip to content

Instantly share code, notes, and snippets.

View briancavalier's full-sized avatar

Brian Cavalier briancavalier

  • Pittsburgh
View GitHub Profile
@briancavalier
briancavalier / instantiate.js
Created December 17, 2010 18:20
Function that creates new object instances using a supplied constructor, new object pass instanceof tests as expected.
// Factory used to create new objects using constructor ctor
var Factory = function Factory(ctor, args) {
return ctor.apply(this, args);
};
// Instantiates new objects using the supplied constructor
// The new objects pass instanceof ctor.
function instantiate(ctor, args) {
Factory.prototype = ctor.prototype;
Factory.prototype.constructor = ctor;
public class FizzBuzz {
public static void main(String... args) {
for (int i = 1; i < 101; i++) {
final boolean fizzy = i % 3 == 0;
final boolean buzzy = i % 5 == 0;
System.out.println(fizzy ? (buzzy ? "FizzBuzz" : "Fizz") : buzzy ? "Buzz" : String.valueOf(i));
}
}
}
@briancavalier
briancavalier / isConstructor.js
Created February 4, 2011 12:31
Testing to see if a function is a constructor without calling it
var undef,
isAConstructor = function Constructor() {};
isAConstructor.prototype.method = function() {};
function notAConstructor() {
return {};
}
function isConstructor(func) {
var result = false;
// Promise implementation based on unscriptable's minimalist Promise:
// https://gist.github.com/814052/
function Promise () {
this._thens = [];
}
Promise.prototype = {
/* This is the "front end" API. */
@briancavalier
briancavalier / tiny closure Promise.js
Created February 7, 2011 12:55
A closure version of my mod (https://gist.github.com/814313) to unscriptable's tiny promise (https://gist.github.com/814052/)
function Promise() {
var callbacks = [],
promise = {
resolve: resolve,
reject: reject,
then: then,
safe: {
then: function safeThen(resolve, reject) {
promise.then(resolve, reject);
}
@briancavalier
briancavalier / semantic tiny Promise.js
Created February 11, 2011 17:00 — forked from unscriptable/tiny Promise.js
An attempt at a more sematic promise API
function Promise () {
this._resolve = [];
this._reject = [];
this._progress = [];
}
Promise.prototype = {
/* This is the "front end" API. */
@briancavalier
briancavalier / always promise decorator.js
Created February 11, 2011 18:46
Decorate a promise with an always() method, kinda like a finally
function makeAlways(promise) {
var always = [];
function doAlways(val) {
var a,
i = 0;
while(a = always[i++]) { a(val); }
}
if(!promise.always) {
@briancavalier
briancavalier / simple-promise-retry.js
Created February 24, 2011 18:35
A few general patterns for retries using promises
function keepTrying(otherArgs, promise) {
promise = promise||new Promise();
// try doing the important thing
if(success) {
promise.resolve(result);
} else {
setTimeout(function() {
keepTrying(otherArgs, promise);
@briancavalier
briancavalier / when.js
Created March 15, 2011 13:46
A better, imho, when() function that normalizes its return value to a promise
// An alternative to dojo.when that always returns a promise
// The problem with dojo.when, imho, is that you have no idea
// what it will return, so if you want to chain anything, you
// end up with lots of nested when()s, like this:
// dojo.when(dojo.when(dojo.when(thing)))
//
// This alternative always returns a promise, so you can write:
// when(thing).then().then().then()
// which is much more intuitive, imho.
//
@briancavalier
briancavalier / whenN.js
Created March 29, 2011 13:28
Fan-in promise resolution
// Return a promise that will resolve when and only
// when N of the supplied promises resolve. The
// resolution value will be an array containing the
// resolution values of the triggering promises.
// TODO: Figure out the best strategy for rejecting.
function whenN(n, promises) {
var toResolve, values, promise;