Skip to content

Instantly share code, notes, and snippets.

@afldcr
Last active August 29, 2015 14:22
Show Gist options
  • Save afldcr/30be86de4b83f617c7fc to your computer and use it in GitHub Desktop.
Save afldcr/30be86de4b83f617c7fc to your computer and use it in GitHub Desktop.
A *better* JavaScript data structure.

Turns out, all you need is a list!

(Oh god what have I done.)

enc('Hello world');

// Output: [[[],[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]],[[],[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]],[[],[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]],[[],[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]],[[],[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[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[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]],[[],[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]],[[],[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]
// Negatives to the left, positives to the right, y'all.
// P.S. Recursion is awesome!
function encInt (x) {
var xArr = _encInt(Math.abs(x));
if (x < 0) return [xArr, []];
else return [[], xArr];
}
function _encInt (x) {
if (x <= 0) return [];
return [_encInt(x-1)];
}
function decInt (x) {
if (x[0].length > x[1].length) return -1 * _decInt(x[0]);
return _decInt(x[1]);
}
function _decInt (x) {
if (x.length === 0) return 0;
return 1 + _decInt(x[0]);
}
// It's really just an array of characters, which are NUMBERS.
// This is basically axiomatic.
function encStr (x) {
if (x.length === 0) return [];
return [encInt(x[0].charCodeAt())].concat(encStr(x.slice(1)));
}
function decStr (x) {
if (x.length === 0) return "";
return String.fromCharCode(decInt(x[0])) + decStr(x.slice(1));
}
// Sometimes, it's nice to be able to just give a function stuff shit
// and get some different stuff out.
function enc (x) {
if (Array.isArray(x)) {
return x.map(enc);
} else if (typeof x === 'object') {
var out = [];
for (var key in x) {
out.append([enc(key), enc(x[key])]);
}
return out;
} else if (Number.isInteger(x)) {
return encInt(x);
} else if (typeof x == 'string') {
return encStr(x);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment