When Meteors Interrupt Your Code
Picture this: you’re tracking radio frequencies for meteor trails, waiting for that sudden burst of reflected signal that means a space rock just ionised the atmosphere 100 kilometres above. Your equipment hums along normally until—crack—the RF signature spikes and your camera must fire immediately. Miss the timing by milliseconds and the trail fades to black.
This is exactly how interrupt handling works in computing. Your processor chugs along executing instructions when suddenly—hardware demands attention right now. The 1980s microprocessor world lived and died by this pattern.
OCaml treats interrupts as exceptional events, literally:
exception MeteorDetected of float
let readings = ref [1.2; 2.1; 4.6]
let read_rf_level () =
match !readings with
| signal :: rest -> readings := rest; signal
| [] -> 0.0
let trigger_camera level =
Printf.printf "camera triggered at %.1f\n" level
let monitor_rf frequency =
ignore frequency;
try while true do
let signal = read_rf_level () in
if signal > 3.5 then raise (MeteorDetected signal)
done
with MeteorDetected level -> trigger_camera level
Kotlin embraces interruption through its coroutine system:
data class MeteorEvent(val intensity: Double)
private val readings = doubleArrayOf(1.2, 2.1, 4.6)
private var readingIndex = 0
class MonitorContext {
val isActive: Boolean
get() = readingIndex < readings.size
}
class FlowEmitter<T>(private val consumer: suspend (T) -> Unit) {
suspend fun emit(value: T) = consumer(value)
}
class Flow<T>(private val producer: suspend (suspend (T) -> Unit) -> Unit) {
suspend fun collect(consumer: suspend (T) -> Unit) = producer(consumer)
}
fun <T> flow(block: suspend FlowEmitter<T>.() -> Unit) = Flow<T> { consumer ->
FlowEmitter(consumer).block()
}
fun currentCoroutineContext() = MonitorContext()
fun readRFLevel() = readings[readingIndex++]
fun triggerCamera(intensity: Double) = println("camera triggered at $intensity")
suspend fun monitorRF() = flow {
while (currentCoroutineContext().isActive) {
val signal = readRFLevel()
if (signal > 3.5) emit(MeteorEvent(signal))
}
}.collect { meteor -> triggerCamera(meteor.intensity) }
The OCaml approach mirrors classic interrupt vectors—normal execution halts completely when the exception fires, jumping to your handler. Clean, decisive, atomic. Kotlin’s flow represents modern thinking: cooperative interruption where your code yields control voluntarily, checking for cancellation.
Both solve the same fundamental problem that plagued early computer designers: how do you handle urgent, unpredictable events without constantly polling? Whether it’s a meteor ionising the ionosphere or a keyboard key being pressed, the pattern remains elegant—let the world interrupt you when something important happens.