Adding a Sequence Rule

Rules define what numbers to generate. Visualizations define how to draw them. This guide shows how to add a new rule.


The Rule Interface

type SequenceRule = {
  name: string;
  id: string;
  description: string;
  maxSteps: number;
  getNext: (params: NextStepParams) => number;
};

NextStepParams gives you:

ParamTypeMeaning
indexnumberCurrent step number (starts at 0)
currentnumberThe previous value in the sequence
seenSet<number>All values generated so far

Step 1: Define the Rule

Edit packages/sequence-renderer/src/core/rules.ts:

const myRule: SequenceRule = {
  name: "My Rule",
  id: "my-rule",
  description: "What it does.",
  maxSteps: 100,
  getNext: ({ index, current, seen }) => {
    // Your math here
    return current + index;
  },
};

Example — Recamán’s Rule:

getNext: ({ index, current, seen }) => {
  const backward = current - index;
  return backward > 0 && !seen.has(backward) ? backward : current + index;
};

Step 2: Register the Rule

Add it to the sequencesRule array in the same file:

export const sequencesRule: SequenceRule[] = [
  // ...existing rules
  myRule,
];

Step 3: Test It

pnpm --filter @repo/sequence-renderer build
pnpm --filter @repo/playground dev

Visit http://localhost:4321/projects/generative/sequences/ and select your new rule from the dropdown.


Checklist

  • Rule has a unique id
  • maxSteps is reasonable (avoid infinite loops)
  • getNext handles edge cases
  • Rule appears in the UI dropdown