Skip to content

Instantly share code, notes, and snippets.

@zmaril
Created December 29, 2011 02:17
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 zmaril/1531201 to your computer and use it in GitHub Desktop.
Save zmaril/1531201 to your computer and use it in GitHub Desktop.
Functions to turn any number into ++[[]]][+[]] Javascript Gobbledygook
//Used to bootstrap way up.
var zero = "+[]";
//Puts the goob into an array.
var intoArray = function(goobString){
return "["+goobString+"]";
};
//Uses the zero variable to grab the zeroth element of the array.
var zeroIndex= function (goobString){
return goobString+intoArray(zero);
};
//Puts the goob into an array, pulls the number back out and then increments.
var increment = function(goobString){
return "++"+zeroIndex(intoArray(goobString));
};
//Takes in a number and does a naive implementation of it.
//Intended for use on digits only, but it can be used on arbitrarily
//long numbers. Watch out for overflowing the recursion limit though.
var digitToStringOfGoob = function(n){
return intoArray(_.reduce(_.range(n), increment, zero));
};
//Takes in a number and spits out goob.
var numberToStringOfGoob= function(n){
return _.map(
String(n).split("")
,digitToStringOfGoob)
.join("+");
};
//Copying in the relevant parts of underscore so that people can just
//copy and paste this to get it to work. This isn't pretty.
var _ = function(obj) { return new wrapper(obj); };
var ArrayProto = Array.prototype, ObjProto = Object.prototype, FuncProto = Function.prototype;
var
nativeForEach = ArrayProto.forEach,
nativeMap = ArrayProto.map,
nativeReduce = ArrayProto.reduce,
nativeReduceRight = ArrayProto.reduceRight,
nativeFilter = ArrayProto.filter,
nativeEvery = ArrayProto.every,
nativeSome = ArrayProto.some,
nativeIndexOf = ArrayProto.indexOf,
nativeLastIndexOf = ArrayProto.lastIndexOf,
nativeIsArray = Array.isArray,
nativeKeys = Object.keys,
nativeBind = FuncProto.bind;
_.map = _.collect = function(obj, iterator, context) {
var results = [];
if (obj == null) return results;
if (nativeMap && obj.map === nativeMap) return obj.map(iterator, context);
each(obj, function(value, index, list) {
results[results.length] = iterator.call(context, value, index, list);
});
if (obj.length === +obj.length) results.length = obj.length;
return results;
};
_.reduce = _.foldl = _.inject = function(obj, iterator, memo, context) {
var initial = arguments.length > 2;
if (obj == null) obj = [];
if (nativeReduce && obj.reduce === nativeReduce) {
if (context) iterator = _.bind(iterator, context);
return initial ? obj.reduce(iterator, memo) : obj.reduce(iterator);
}
each(obj, function(value, index, list) {
if (!initial) {
memo = value;
initial = true;
} else {
memo = iterator.call(context, memo, value, index, list);
}
});
if (!initial) throw new TypeError('Reduce of empty array with no initial value');
return memo;
};
_.range = function(start, stop, step) {
if (arguments.length <= 1) {
stop = start || 0;
start = 0;
}
step = arguments[2] || 1;
var len = Math.max(Math.ceil((stop - start) / step), 0);
var idx = 0;
var range = new Array(len);
while(idx < len) {
range[idx++] = start;
start += step;
}
return range;
};
console.log("Now watch as a simple number is turned into a long string of silly nonsense!");
console.log("We call numberToStringOfGoob(31415):"+numberToStringOfGoob(31415));
console.log("This makes no sense! But yet what happens when we call eval(numberToStringOfGoob(31415))?");
console.log("This! "+ eval(numberToStringOfGoob(31415)));
console.log("Will the wonders ever stop? 31415==eval(numberToStringOfGoob(31415)):"+(31415==eval(numberToStringOfGoob(31415))));
console.log("Type coercion FTW");
@zmaril
Copy link
Author

zmaril commented Dec 29, 2011

N.B. This depends on underscore.js for the functional parts.

EDIT: And now it doesn't.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment