This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var undef, | |
isAConstructor = function Constructor() {}; | |
isAConstructor.prototype.method = function() {}; | |
function notAConstructor() { | |
return {}; | |
} | |
function isConstructor(func) { | |
var result = false; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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. */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Promise() { | |
var callbacks = [], | |
promise = { | |
resolve: resolve, | |
reject: reject, | |
then: then, | |
safe: { | |
then: function safeThen(resolve, reject) { | |
promise.then(resolve, reject); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Promise () { | |
this._resolve = []; | |
this._reject = []; | |
this._progress = []; | |
} | |
Promise.prototype = { | |
/* This is the "front end" API. */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function makeAlways(promise) { | |
var always = []; | |
function doAlways(val) { | |
var a, | |
i = 0; | |
while(a = always[i++]) { a(val); } | |
} | |
if(!promise.always) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function keepTrying(otherArgs, promise) { | |
promise = promise||new Promise(); | |
// try doing the important thing | |
if(success) { | |
promise.resolve(result); | |
} else { | |
setTimeout(function() { | |
keepTrying(otherArgs, promise); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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. | |
// |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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; | |