By Alvin Alexander. Last updated: May 24, 2020
The book, Advanced Scala with Cats, has a nice little function you can use to run a block of code “slowly”:
def slowly[A](body: => A) = try body finally Thread.sleep(100)
I’d never seen a try/finally block written like that (without a catch
clause), so it was something new for the brain.
In the book they run a factorial
method slowly, like this:
slowly(factorial(n - 1).map(_ * n))
FWIW, you can modify slowly
to pass in the length of time to sleep, like this:
def slowly[A](body: => A, sleepTime: Long) = try body finally Thread.sleep(sleepTime)