Skip to content

TimeIsAnInput

Ben Christel edited this page Dec 20, 2020 · 2 revisions

"Time is an input" is one way to View certain Procedural algorithms that depend on the passage of WallClockTime (e.g. because they sleep). When such algorithms are encoded in a way that makes time an explicit input, they can be invoked by pure Functions that merely simulate the passage of time. This makes them much easier to UnitTest.

The view is inspired by / related to static core CPU designs that consider the clock as a separate component from the logical circuitry of the CPU. In such designs, the CPU logic is purely functional; the output voltages on various pins are at every moment determined solely by the voltages on the input pins, and if the inputs are held constant over time the outputs will also be constant. See Ben Eater's series of videos on the 6502 processor for more.

A simple way of implementing a "time is an input" system is as an Object with a Method that increments time:

function Pong() {
  // the ball's position
  let ballX = 0
  let ballY = 0
  // the ball's velocity
  let ballVX = 1
  let ballVY = 1

  return {tick}

  function tick(dt) {
    ballX += ballVX * dt
    ballY += ballVY * dt
  }

  // ...
}
Clone this wiki locally