Timers in Idris
Our first example demonstrates how to use timers in Idris. Timers allow us to execute code at some point in the future or repeatedly at some interval. We’ll focus on timers in this example.
In this Idris example, we’re using the System.Concurrency
module to simulate timers. We use fork
to start asynchronous computations and sleep
to introduce delays.
For the first timer, we simply fork a computation that sleeps for 2 seconds and then prints a message. We use wait
to block until this computation completes.
For the second timer, we demonstrate how to “cancel” a timer. We use an IORef
to hold a cancellation flag. The timer checks this flag before executing its action. We set the flag to simulate stopping the timer.
To run this program, save it as timers.idr
and use the Idris compiler:
The first timer will fire ~2 seconds after we start the program, but the second timer’s action is prevented from executing due to our “cancellation” mechanism.
Note that Idris doesn’t have built-in timer functionality like Go, so we’ve simulated it using concurrency primitives. In a real-world scenario, you might want to use a more sophisticated timing library if available.