Skip to content

Instantly share code, notes, and snippets.

View fitzgen's full-sized avatar
🌴
On vacation

Nick Fitzgerald fitzgen

🌴
On vacation
View GitHub Profile
var fibonacci = function () {
var memo = [0, 1];
var fib = function (n) {
var result = memo[n];
if (typeof result !== 'number') {
result = fib(n-1) + fib(n-2);
memo[n] = result;
}
return result;
}
function quote(fn) {
return typeof(fn.name) === "string" ?
fn.name :
fn.toString()
.replace("function ", "")
.replace(/\([\s\S]*/, "");
}
function funCall(fn) {
var args = Array.prototype.slice.call(arguments, 1),
div = function () {
var args = Array.prototype.slice.call(arguments, 0);
var attrs = {};
var txt = args.map(function (arg) {
if (typeof arg === "string") {
return arg;
} else {
for (attr in arg) {
attrs[attr] = arg[attr];
}
// Based on Nicholas C Zakas' function in High Performance Javascript
function batchProcess(items, process, callback) {
// Create a copy of the original items array so that our side effects
// (calling .shift()) don't pollute anything outside of this scope.
var todo = items.slice(0);
setTimeout(function () {
var start = +new Date, result = true;
> (function (arg) { return arg === undefined; }()) // Missing arguments are undefined
true
> var items = []
undefined
> items.push() // Pushing implicitly undefined item doesn't work
0
> items
[]
> items.push(undefined) // What about explicitly pushing undefined?
1
var
test = test || null,
ok = ok || null,
deepEqual = deepEqual || null,
assert = null;
// If we are not running in Qunit in the browser, create a shim from the Qunit API
// to the CommonJS API.
if (typeof exports !== "undefined") {
assert = require("assert");
var FILE = require("file");
require.paths.unshift(FILE.join(FILE.dirname(module.path),
"jsmacros/vendor/jison/lib"));
var Parser = require("jison").Parser;
require.paths.shift();
var grammar = {
lex: {
rules: [
//@use bind.jsm
function returnsMultipleVals () {
return ["foo", "bar"];
}
var foo, bar;
@fitzgen
fitzgen / operations.js
Created February 18, 2011 01:25
Uses a slightly modified version of the Levenshtien Distance algorithm (also known as "edit distance") to calculate the set of the fewest operations which will change string s in to string t.
# Output of the example which is console.logged at the bottom of operations.js:
$ node operations.js
To change 'kitten' in to 'sitting':
[ [ 'delete', 'k' ]
, [ 'insert', 's' ]
, [ 'retain', 1 ]
, [ 'retain', 1 ]
, [ 'retain', 1 ]
, [ 'delete', 'e' ]
@fitzgen
fitzgen / gist:749904
Last active April 26, 2024 20:08
Gerard Paapu's TCO. Works with mutual recursion.
(function () {
// conceal the Thunk class to avoid
// so that the only way to make one is
// to call Function::thunk
function Thunk(fn, args) {
this.fn = fn;
this.args = args;
}
Thunk.prototype.force = function () {