Chapter 16 of 22 Part 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.

Download Chapter ZIP

block-01.js

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('');
}
Download .js

block-02.js

Printed book snippet (byte-identical) · 113 bytes · SHA-256: d9cea2b65a1c…
Standalone runnable

Dependencies: None (standard ES2022 JavaScript). Runs directly in Node.js or the isolated browser sandbox and logs output.

const text = 'AAAAAABBBCC';
const packed = pack(text);
console.log(packed);
console.log(unpack(packed) === text);
Download .js

block-03.js

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;
}
Download .js

block-04.js

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;
}
Download .js

block-05.js

Printed book snippet (byte-identical) · 201 bytes · SHA-256: fca4d9732e97…
Standalone runnable

Dependencies: None (standard ES2022 JavaScript). Runs directly in Node.js or the isolated browser sandbox and logs output.

const message = 'AAAAAABBBCC';
const tree = makeTree(message);
const codes = makeCodes(tree);
const bits = encode(message, codes);
console.log(bits.length);
console.log(decode(bits, tree) === message);
Download .js

block-06.js

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;
}
Download .js

block-07.js

Printed book snippet (byte-identical) · 160 bytes · SHA-256: e9c6f339083e…
Standalone runnable

Dependencies: None (standard ES2022 JavaScript). Runs directly in Node.js or the isolated browser sandbox and logs output.

const original = [10, 50, 100, 150, 200, 245];
const reduced = original.map(v=>quantize(v, 4));
console.log(reduced);
console.log(meanError(original, reduced));
Download .js