Skip to content

Instantly share code, notes, and snippets.

@RoyalIcing
Created March 23, 2022 03:55
Show Gist options
  • Save RoyalIcing/78eed77ca2e1908e0b90d4bf616fea1a to your computer and use it in GitHub Desktop.
Save RoyalIcing/78eed77ca2e1908e0b90d4bf616fea1a to your computer and use it in GitHub Desktop.
Modelling Formula One qualifying as a state machine
const S1Time = Symbol('S1Time');
function DriverTime() {
function* NoTime() {
yield on('sector1Set', SectorTime, id('Sector1'));
yield on('sector2Set', SectorTime, id('Sector2'));
yield on('sector3Set', SectorTime, id('Sector3'));
}
function* SectorTime() {
const previousBest = yield read('bestTime');
const newTime = yield read('duration');
const runIndex = yield read('runIndex');
yield cond(previousBest == null, set('bestTime', newTime));
yield cond(newTime < previousBest, set('bestTime', newTime));
// Or:
yield new Map([
[previousBest == null, set('bestTime', newTime)],
[newTime < previousBest, set('bestTime', newTime)],
]);
}
return NoTime;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment