Skip to content

Instantly share code, notes, and snippets.

@jcmoore
Created December 15, 2013 10:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jcmoore/7971107 to your computer and use it in GitHub Desktop.
Save jcmoore/7971107 to your computer and use it in GitHub Desktop.
Xient -- pooled helper functions to facilitate returning multiple values from a method, curry-like partial evaluation, and joining asynchronous callbacks
var Xient = (function () {
var backdoor = {};
var amount = 0;
var collection = [function () {}]; // force the most memory intensive storage at initialization
collection.pop();
var Nomad = function () {
var access = backdoor;
var routine = (amount > 0) ? collection[amount-- - 1] : null;
var status = arguments.length;
var context = (status > 1) ? arguments : null;
var method = (status > 0) ? arguments[status - 1] : null;
status = ((status > 0) && (arguments[0] === access)) ? null : 0;
if (status === null) {
context = null;
}
if (routine) {
collection[amount] = null;
routine(access, context, method, status); // reset
} else {
routine = function () {
if (arguments.length > 3 &&
arguments[0] === access) {
context = arguments[1];
method = arguments[2];
status = arguments[3];
return;
}
var s = status;
if (s) {
throw "Cannot invoke a recycled Nomad.";
return;
}
status = 1;
if (s === null) {
method(arguments, null);
} else {
method(context, arguments);
}
context = null;
method = null;
var c = collection;
if (c.length > amount) {
c[amount++] = routine;
} else {
amount = c.push(routine);
}
};
}
return routine;
};
var Camp = function (penultimate) {
return Nomad(backdoor, penultimate);
};
var Drifter = function (optional) {
var limited = (optional === backdoor);
var access = backdoor;
var puddle = null;
var pool = null;
var place = 0;
return function () {
var Callback = Nomad;
var auth = access;
var _;
var bindle = puddle || ((place > 0) ? pool[place-- - 1] : null);
var storage = null;
var cache = null;
var mark = 0;
var pending = 0;
var status = 0;
var context = null;
var method = null;
if (!limited) {
status = arguments.length;
if (status > 1) {
context = arguments;
}
if (status > 0) {
method = arguments[status - 1];
}
status = 0;
}
if (bindle) {
puddle ? (puddle = null) : (pool[place] = null);
bindle.__(auth, context, method); // reset
} else {
// get
bindle = function (index) {
var count = mark;
count = (count === null) ? 1 : count;
index = index || ((index === null) ? null : 0);
if (index === null) {
return count;
} else if (index < 0) {
return;
} else if (index === 0) {
return storage;
} else if (index < count) {
return cache[index];
}
};
// set
bindle.$$ = null;
bindle.$$ = function () {
if (status) {
throw "Cannot use a recycle Bindle.";
}
var flag = false;
if (mark === null) {
flag = true;
mark = 0;
}
var params = arguments;
var count = params.length;
if (count < 1) {
params = null;
}
var c = cache;
if (!flag) {
mark = null;
storage = params;
} else if (!c) {
mark = 2;
cache = [storage, params];
} else {
if (flag) {
mark = 0;
c[mark++] = storage;
}
if (c.length > mark) {
c[mark++] = params;
} else {
mark = c.push(params);
}
}
return bindle;
};
if (!limited) {
// notify
_ = function (ctx, params) {
var index = ctx[0];
var b;
var m;
var c;
if (pending > 0) {
if ((c = cache)) {
c[index] = params;
}
if (index === 0) {
storage = params;
}
pending--;
if (pending === 0) {
b = bindle;
m = method;
if (m) {
m(b, context);
}
b.__();
}
}
};
// callback
bindle.$_ = null;
bindle.$_ = function () {
var index = mark;
if (index === null) {
index = 1;
}
pending++;
bindle.$$();
return Callback(index, _);
};
} else {
bindle.$_ = null;
}
// recycle -- not mandatory, but has memory implications when left uncalled
bindle.__ = null;
bindle.__ = function (code, ctx, mth) {
if (code === auth) {
// reset
context = ctx;
method = mth;
status = 0;
return;
}
if (status) {
return;
}
status = 1;
var p = pool;
var b;
if (!p) {
b = puddle;
if (!b) {
puddle = bindle;
} else {
pool = [b, bindle];
puddle = null;
place = 2;
}
} else {
if (p.length > place) {
p[place++] = bindle;
} else {
place = p.push(bindle);
}
}
var l = mark;
var i;
if (l !== null) {
for (i = 0; i < l; i++) {
cache[i] = null;
}
}
mark = 0;
storage = null;
pending = 0;
};
}
return bindle;
};
};
var Transient = function () {
return Drifter(backdoor);
};
Transient.Camp = Camp;
Transient.Nomad = Nomad;
Transient.Drifter = Drifter;
return Transient;
}) ();
var x__ = Xient();
var x = function () {
var a = 4;
var b = 5;
var c = 6;
return x__().$$(a, b, c);
};
(function () {
var bindle = x();
var params = bindle();
var a = params[0];
var b = params[1];
var c = params[2];
bindle.__();
console.log(JSON.stringify([a, b, c]));
}) ();
var y__ = Xient.Drifter();
var y = function (mth) {
var i = 7;
var j = 8;
var k = 9;
//fs.readFile(path, "utf", y__(mth).$$(i, j, k).$_());
var error = null;
var text = "hello world";
y__(mth).$$(i, j, k).$_()(error, text);
};
var z__ = Xient.Drifter();
var z = function (mth) {
var u = 3;
var v = 2;
var w = 1;
y(z__(mth).$$(u, v, w).$_());
};
(function () {
z(function (bindle, context) {
var count = bindle(null);
var paramsUVW = bindle(0);
var paramsBindleContext = bindle(1);
var u = paramsUVW[0];
var v = paramsUVW[1];
var w = paramsUVW[2];
bindle = paramsBindleContext[0];
context = paramsBindleContext[1];
var paramsIJK = bindle(0);
var paramsErrorText = bindle(1);
var i = paramsIJK[0];
var j = paramsIJK[1];
var k = paramsIJK[2];
var error = paramsErrorText[0];
var text = paramsErrorText[1];
console.log(JSON.stringify([u, v, w, i, j, k, error, text]));
});
}) ();
Xient.Nomad("x", "y", "z",
Xient.Nomad("i", "j", "k",
Xient.Nomad("a", "b", "c", function (abc, ijk__xyz_uvw) {
var ijk = ijk__xyz_uvw[0];
var xyz_uvw = ijk__xyz_uvw[1];
var xyz = xyz_uvw[0];
var uvw = xyz_uvw[1];
console.log(JSON.stringify([
[uvw[0], uvw[1], uvw[2]],
[xyz[0], xyz[1], xyz[2]],
[ijk[0], ijk[1], ijk[2]],
[abc[0], abc[1], abc[2]],
]));
}))) ("u", "v", "w");
(function () {
var that = this;
var n = Xient.Nomad(that, 1, 2, 3, function (that123, _456___789__errorText_null) {
var pair = arguments;
that123 = pair[0];
pair = pair[1];
var _456 = pair[0];
pair = pair[1];
var _789 = pair[0];
pair = pair[1];
var errorText = pair[0];
pair = pair[1]; // null terminator -- Xient.Camp
console.log(JSON.stringify([
that123[0].toString(),
[that123[1], that123[2], that123[3],
_456[0], _456[1], _456[2],
_789[0], _789[1], _789[2]
],
[errorText[0], errorText[1]],
]));
});
var a = function (nomad) {
nomad = Xient.Nomad(4, 5, 6, nomad);
b(nomad);
};
var b = function (nomad) {
nomad = Xient.Nomad(7, 8, 9, nomad);
c(nomad);
};
var c = function (nomad) {
//fs.readFile(path, "utf", Xient.Camp(nomad));
var error = null;
var text = "'sup earth";
Xient.Camp(nomad) (error, text);
};
a(n);
}) ();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment