Files
functional-programming-snake/js/src/stream.js
T
Lucas WintherandClaude Fable 5.2 edd57f0148 feat(course): audit fixes and twelve extensions (Modules 7b, 12, 13)
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>
2026-11-11 12:05:29 +01:00

77 lines
3.6 KiB
JavaScript

import { todo } from './todo.js'
/** @import { Point, Input, Stream } from './types' */
/**
* An event stream as a pure automaton (a Mealy machine).
* `Stream<A> = { step: input => [outputs, nextStream] }`: feed one input, get the (possibly empty) array
* of outputs it produced and the stream to use for the next input. Nothing is mutated, so a stream value
* can be run any number of times with the same results.
* Inputs: `{tag:'Time', t}`, `{tag:'KeyDown', key}`, `{tag:'KeyUp', key}`, and from Module 13 `{tag:'Click', x, y}`
* (a mouse click, in canvas pixels). Every stream ignores the kinds of input it does not use.
*/
/** Emits the `key` of every KeyDown input; ignores everything else.
* @type {Stream<string>} */
export const keyDowns = { step: () => todo('m08') }
/** Emits the `key` of every KeyUp input; ignores everything else.
* @type {Stream<string>} */
export const keyUps = { step: () => todo('m08') }
/**
* (m13) Emits `{x, y}` for every Click input; ignores everything else.
* Given: nothing to write here. It is the third source, built exactly like your `keyDowns`.
* @type {Stream<Point>}
*/
export const clicks = { step: (i) => [i.tag === 'Click' ? [{ x: i.x, y: i.y }] : [], clicks] }
/**
* On `Time t`, emits every multiple `k*ms` with `last < k*ms <= t`, ascending, where `last` starts at 0
* and becomes `t` afterwards. A big time jump can emit several values. Ignores key inputs.
* Hint: write a private helper that carries `last`.
* @type {(ms: number) => Stream<number>}
*/
export const interval = (ms) => todo('m08')
/** Apply `f` to every output of `s`.
* @type {<A, B>(s: Stream<A>, f: (a: A) => B) => Stream<B>} */
export const map = (s, f) => todo('m08')
/** Keep only the outputs of `s` for which `p` is true.
* @type {<A>(s: Stream<A>, p: (a: A) => boolean) => Stream<A>} */
export const filter = (s, p) => todo('m08')
/** Apply `f` to every output of `s`; `f` returns a value or `null`, and nulls are dropped.
* @type {<A, B>(s: Stream<A>, f: (a: A) => B | null) => Stream<B>} */
export const filterMap = (s, f) => todo('m08')
/** Both streams step on every input; the outputs of `a` come before the outputs of `b`.
* @type {<A>(a: Stream<A>, b: Stream<A>) => Stream<A>} */
export const merge = (a, b) => todo('m08')
/** Fold the outputs of `s` into an accumulator, starting from `z`; emits each new accumulator (not `z` itself).
* @type {<A, B>(s: Stream<A>, f: (acc: B, a: A) => B, z: B) => Stream<B>} */
export const scan = (s, f, z) => todo('m08')
/**
* Emit `v` once, before the outputs of the first step; later steps are unaffected.
* Note: streams only act when an input arrives, so if no input ever arrives, `v` is never emitted.
* @type {<A>(s: Stream<A>, v: A) => Stream<A>}
*/
export const startWith = (s, v) => todo('m08')
/**
* (m10) Limit how far one Time input can move the clock that `s` sees, so a long silence (a background tab)
* does not turn into hundreds of beats at once.
* `s` gets a clock of its own, starting at 0. On `Time t` that clock advances by `t - last`, but by at most `maxGap`
* and never by less than 0 (time does not run backwards), where `last` is the `t` of the previous Time input
* (0 at the start). `s` is then stepped with `{tag:'Time', t: clock}`. Key inputs are passed to `s` unchanged.
* Hint: a private helper that carries `last` and `clock`, like the one for `interval`.
* @type {<A>(s: Stream<A>, maxGap: number) => Stream<A>}
*/
export const clampTime = (s, maxGap) => todo('m10')
/** Feed all inputs through the stream and collect every output, in order. A pure fold (reduce).
* @type {<A>(s: Stream<A>, inputs: Input[]) => A[]} */
export const run = (s, inputs) => todo('m08')