Files
functional-programming-snake/js/src/runtime.js
T

25 lines
1.2 KiB
JavaScript

import { todo } from './todo.js'
/**
* The single impure place in the course (with browser.js and main.js): everything else only builds values.
* A Platform is `{ fillRect(x,y,w,h,color), fillText(x,y,text,color,size,align), randomSeed() -> number,
* startClock(onTime: t => void), onKeyDown(cb: key => void), onKeyUp(cb: key => void) }`.
* Here `let`, loops and mutation are allowed. Keep it tiny.
*/
/**
* Interpret an IO against a platform and return its final value.
* `Draw` of a Rect -> `platform.fillRect(x, y, w, h, color)`; of a Text -> `platform.fillText(x, y, text, color, size, align)`;
* `RandomSeed` -> `platform.randomSeed()`. The result of each op is passed to `io.k`.
* Must be a loop, not recursion: a long sequence of draws must not overflow the stack.
*/
export const runIO = (io, platform) => todo('m09')
/**
* Run the program: `runIO(main)` gives the stream; keep the current stream in the one mutable variable.
* Register `platform.onKeyDown` / `onKeyUp` / `startClock` so that each event steps the stream with the matching
* input (`{tag:'KeyDown', key}`, `{tag:'KeyUp', key}`, `{tag:'Time', t}`), stores the next stream,
* and runs every IO the step emitted, in order.
*/
export const runApp = (main, platform) => todo('m09')