Type a Traffic Light State Machine
Write the code, see it run, pass the checks.
The problem
- hard
- Code
- Pro
A traffic light is red, green or yellow, and moves on through events: go turns red to green, slow turns green to yellow, stop turns yellow to red, and emergency turns any light red. Export a Light type and next(state, event), which returns the new light and throws for an event the light doesn't take. Type next so only that light's events compile and the result is the exact next light (next of red and go is typed green, not just Light); for a light known only as Light, only emergency is allowed.
Your code must pass
- Follows each transition
- Turns any light red in an emergency
- Throws for an event the light doesn't take
- Has no type errors
- Light is the three colours
- Returns the exact next light
- Rejects an event the light doesn't take
- Allows only emergency for any light