Chapter 16 of 22Part IV: Data, Media & Machine Intelligence
Making Big Things Small
7 Verified Book Snippets
Every .js file below is byte-identical (SHA-256 verified) to the printed code in Volume 1. Runnable blocks execute inside an isolated sandboxed iframe and Web Worker with a 2-second timeout and zero network access.
Printed book snippet (byte-identical) · 284 bytes · SHA-256: 82a855b2b17a…
Standalone runnable
Dependencies: None (standard ES2022 JavaScript). Defines pure JavaScript functions/data; runs cleanly in the sandbox (add a console.log call in your own test to inspect return values).
function pack(text) {
const runs = [];
for (const char of text) {
const last = runs[runs.length - 1];
if (last && last[0] === char) last[1]++;
else runs.push([char, 1]);
}
return runs;
}
function unpack(runs) {
return runs.map(([c,n]) => c.repeat(n)).join('');
}
Printed book snippet (byte-identical) · 652 bytes · SHA-256: 9c247890c3b5…
Standalone runnable
Dependencies: None (standard ES2022 JavaScript). Defines pure JavaScript functions/data; runs cleanly in the sandbox (add a console.log call in your own test to inspect return values).
function makeTree(text) {
const counts = new Map();
for (const c of text)
counts.set(c, (counts.get(c) || 0) + 1);
const q = [...counts].map(([c,n]) => ({c,n}));
while (q.length > 1) {
q.sort((a,b) => a.n - b.n);
const left = q.shift(), right = q.shift();
q.push({n:left.n+right.n, left, right});
}
return q[0] || null;
}
function makeCodes(tree) {
const codes = new Map();
function visit(node, path) {
if (!node) return;
if (node.c !== undefined)
codes.set(node.c, path || '0');
else {
visit(node.left, path+'0');
visit(node.right, path+'1');
}
}
visit(tree, '');
return codes;
}
Printed book snippet (byte-identical) · 604 bytes · SHA-256: 314373fcb84a…
Standalone runnable
Dependencies: None (standard ES2022 JavaScript). Defines pure JavaScript functions/data; runs cleanly in the sandbox (add a console.log call in your own test to inspect return values).
function encode(text, codes) {
return [...text].map(c=>codes.get(c)).join('');
}
function decode(bits, tree) {
if (!tree) {
if (bits) throw Error('Unexpected bits');
return '';
}
if (tree.c !== undefined) {
if (/[^0]/.test(bits)) throw Error('Bad bit');
return tree.c.repeat(bits.length);
}
let node=tree, text='';
for (const b of bits) {
if (b!=='0' && b!=='1') throw Error('Bad bit');
node=b==='0' ? node.left : node.right;
if (node.c !== undefined) {
text+=node.c; node=tree;
}
}
if (node!==tree) throw Error('Incomplete code');
return text;
}
Printed book snippet (byte-identical) · 257 bytes · SHA-256: c547ed7e1fc3…
Standalone runnable
Dependencies: None (standard ES2022 JavaScript). Defines pure JavaScript functions/data; runs cleanly in the sandbox (add a console.log call in your own test to inspect return values).
function quantize(value, levels) {
const step = 255 / (levels - 1);
return Math.round(Math.round(value/step)*step);
}
function meanError(original, reduced) {
return original.reduce((sum,v,i) =>
sum + Math.abs(v-reduced[i]), 0) / original.length;
}