Audit pass: - Frame rule counts frames since the last move, so every step lasts exactly movesEvery(score) frames, also right after a speed-up - clampTime + MAX_GAP_MS: a hidden tab no longer replays the whole gap - Pong Restart keeps held keys - Fact corrections (mulberry32 output coverage, Haskell IO, uniform) - New fixture cases from a mutation analysis Extensions (all three tracks, shared fixtures, course text): - Turn queue (Exercise 10.6), persisted high score (10.7), replay viewer (10.8), play() end-to-end helper (9.5) - Optional State (4.7) and optional Module 7b, stack-safe IO - Module 12, property-based testing with generators, shrinking, invariants and a model-based oracle - Module 13, Conway's Life with a Click event - JS type checking via jsconfig, JSDoc and types.d.ts - Maintainer tools: scripts/mutate.mjs, scripts/typecheck-js.sh, Playwright end-to-end suite (e2e/, scripts/e2e.sh) Docs: docs/contract.md is the canonical contract; specs and build log under docs/superpowers. Verified: JS 728, Scala 689, F# 691 unit tests; stubs and progression M01-M13 and M07b; 176 end-to-end tests on Chromium and WebKit. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
54 lines
2.3 KiB
JavaScript
54 lines
2.3 KiB
JavaScript
// m07 io: effects as values. An IO is a plain-data description of a program; nothing runs until interpreted.
|
|
// {tag:'Pure', value} - done, with a value
|
|
// {tag:'Effect', op, k} - perform `op`, then continue with `k(result)`
|
|
/** @import { Op, Shape, Scene, IO, Trace } from './types' */
|
|
|
|
/** @type {<A>(a: A) => IO<A>} */
|
|
export const pure = (value) => ({ tag: 'Pure', value })
|
|
|
|
/** @type {<A>(op: Op) => IO<A>} */
|
|
export const effect = (op) => ({ tag: 'Effect', op, k: pure })
|
|
|
|
// Pure: apply f now. Effect: keep the op, and extend the continuation so f runs after k.
|
|
// Nothing is performed here, we only build a bigger description.
|
|
/** @type {<A, B>(io: IO<A>, f: (a: A) => IO<B>) => IO<B>} */
|
|
export const flatMap = (io, f) =>
|
|
io.tag === 'Pure'
|
|
? f(io.value)
|
|
: { tag: 'Effect', op: io.op, k: (result) => flatMap(io.k(result), f) }
|
|
|
|
/** @type {<A, B>(io: IO<A>, f: (a: A) => B) => IO<B>} */
|
|
export const map = (io, f) => flatMap(io, (a) => pure(f(a)))
|
|
|
|
/** @type {(ios: IO<any>[]) => IO<undefined>} */
|
|
export const sequence = (ios) =>
|
|
ios.reduceRight((rest, io) => flatMap(io, () => rest), pure(undefined))
|
|
|
|
/** @type {(shape: Shape) => IO<undefined>} */
|
|
export const draw = (shape) => effect({ tag: 'Draw', shape })
|
|
|
|
/** @type {IO<number>} */
|
|
export const randomSeed = effect({ tag: 'RandomSeed' })
|
|
|
|
/** @type {(scene: Scene) => IO<undefined>} */
|
|
export const drawScene = (scene) => sequence(scene.map(draw))
|
|
|
|
// m10: the stored best score. Reading it answers a number; saving answers nothing, like a draw.
|
|
/** @type {IO<number>} */
|
|
export const loadBest = effect({ tag: 'LoadBest' })
|
|
|
|
/** @type {(score: number) => IO<undefined>} */
|
|
export const saveBest = (score) => effect({ tag: 'SaveBest', score })
|
|
|
|
// Answer each op with a canned result, collecting the ops as we go (recursion, no mutation).
|
|
// `best` (m10) answers LoadBest, the way `seed` answers RandomSeed; it defaults to 0 so m07 calls stay valid.
|
|
const answer = (op, seed, best) =>
|
|
op.tag === 'RandomSeed' ? seed : op.tag === 'LoadBest' ? best : undefined
|
|
|
|
/** @type {<A>(io: IO<A>, seed: number, best?: number) => Trace<A>} */
|
|
export const trace = (io, seed, best = 0) => {
|
|
if (io.tag === 'Pure') return { result: io.value, ops: [] }
|
|
const rest = trace(io.k(answer(io.op, seed, best)), seed, best)
|
|
return { result: rest.result, ops: [io.op, ...rest.ops] }
|
|
}
|