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

47 lines
2.0 KiB
JavaScript

import { todo } from './todo.js'
/**
* 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}`.
*/
/** Emits the `key` of every KeyDown input; ignores everything else. */
export const keyDowns = { step: () => todo('m08') }
/** Emits the `key` of every KeyUp input; ignores everything else. */
export const keyUps = { step: () => todo('m08') }
/**
* 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`.
*/
export const interval = (ms) => todo('m08')
/** Apply `f` to every output of `s`. */
export const map = (s, f) => todo('m08')
/** Keep only the outputs of `s` for which `p` is true. */
export const filter = (s, p) => todo('m08')
/** Apply `f` to every output of `s`; `f` returns a value or `null`, and nulls are dropped. */
export const filterMap = (s, f) => todo('m08')
/** Both streams step on every input; the outputs of `a` come before the outputs of `b`. */
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). */
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.
*/
export const startWith = (s, v) => todo('m08')
/** Feed all inputs through the stream and collect every output, in order. A pure fold (reduce). */
export const run = (s, inputs) => todo('m08')