Tracks
/
Roc
Roc
/
Exercises
/
Split-Second Stopwatch
Split-Second Stopwatch

Split-Second Stopwatch

Medium

Introduction

You've always run for the thrill of it — no schedules, no timers, just the sound of your feet on the pavement. But now that you've joined a competitive running crew, things are getting serious. Training sessions are timed to the second, and every split second counts. To keep pace, you've picked up the Split-Second Stopwatch — a sleek, high-tech gadget that's about to become your new best friend.

Instructions

Your task is to build a stopwatch to keep precise track of lap times.

The stopwatch uses four commands (start, stop, lap, and reset) to keep track of:

  1. The current lap's tracked time
  2. Previously recorded lap times

What commands can be used depends on which state the stopwatch is in:

  1. Ready: initial state
  2. Running: tracking time
  3. Stopped: not tracking time
Command Begin state End state Effect
Start Ready Running Start tracking time
Start Stopped Running Resume tracking time
Stop Running Stopped Stop tracking time
Lap Running Running Add current lap to previous laps, then reset current lap
Reset Stopped Ready Reset current lap and clear previous laps

Time literals with from_quote

The Time.from_quote function parses a string formatted as "HH:MM:SS" into a Time. This makes it possible to write code such as this:

time : Time
time = "00:01:23"

This code implicitly calls Time.from_quote("00:01:23") at compile time. Running code at compile time using this approach (or other methods described below) can potentially speed up the program since it doesn't need to handle parsing and data validation at runtime. Moreover, it ensures that invalid data (such as an incorrect date format) is rejected at compile time rather than at runtime, making your app more robust. Lastly, it makes the code nice and readable. For example, here's one of the tests in this exercise:

## start initiates time tracking for current lap
expect {
	result = Stopwatch.create()
		.start()?
		.advance_time("00:00:05")
		|> expect_current_lap("00:00:05")

	result.is_ok()
}

However, someone reading this code might assume that the advance_time function takes a Str when in fact it takes a Time. This can lead to confusion and errors. For this reason, you may prefer the slightly longer but more explicit format: "00:00:05".Time which also calls from_quote at compile time.

In your own programs, you may prefer to use from_str instead:

Time := { ... }.{
    from_str : Str -> Try(Time, [InvalidTime])
    from_str = |time_str| ...
}

You can also call from_str (or any other pure function) at compile time, like this:

Ok(time) = Time.from_str("00:01:23")

Note: Placing this definition at the top level makes it run at compile time (since the function is pure and its input is known at compile time). Inside a function, you can use from_str at runtime.

Edit via GitHub The link opens in a new window or tab
Roc Exercism

Ready to start Split-Second Stopwatch?

Sign up to Exercism to learn and master Roc with 139 exercises, and real human mentoring, all for free.