summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormo8it <mo8it@proton.me>2024-08-29 01:59:04 +0200
committermo8it <mo8it@proton.me>2024-08-29 01:59:04 +0200
commitfd2bf9f6f66f9ff680925cf0bea86c14c6da07c9 (patch)
tree2ef1c3f79b48340631b9a14580335bc43f8704ed
parentfc1f9f012431b129dea850443b6b3b8a760a45e1 (diff)
Simplify next_pending_exercise_ind
-rw-r--r--src/app_state.rs35
1 files changed, 16 insertions, 19 deletions
diff --git a/src/app_state.rs b/src/app_state.rs
index ef2f874..058352a 100644
--- a/src/app_state.rs
+++ b/src/app_state.rs
@@ -313,25 +313,22 @@ impl AppState {
// Return the index of the next pending exercise or `None` if all exercises are done.
fn next_pending_exercise_ind(&self) -> Option<usize> {
- if self.current_exercise_ind + 1 == self.exercises.len() {
- // The last exercise is done.
- // Search for exercises not done from the start.
- return self.exercises[..self.current_exercise_ind]
- .iter()
- .position(|exercise| !exercise.done);
- }
-
- // The done exercise isn't the last one.
- // Search for a pending exercise after the current one and then from the start.
- match self.exercises[self.current_exercise_ind + 1..]
- .iter()
- .position(|exercise| !exercise.done)
- {
- Some(ind) => Some(self.current_exercise_ind + 1 + ind),
- None => self.exercises[..self.current_exercise_ind]
- .iter()
- .position(|exercise| !exercise.done),
- }
+ let next_ind = self.current_exercise_ind + 1;
+ self.exercises
+ // If the exercise done isn't the last, search for pending exercises after it.
+ .get(next_ind..)
+ .and_then(|later_exercises| {
+ later_exercises
+ .iter()
+ .position(|exercise| !exercise.done)
+ .map(|ind| next_ind + ind)
+ })
+ // Search from the start.
+ .or_else(|| {
+ self.exercises[..self.current_exercise_ind]
+ .iter()
+ .position(|exercise| !exercise.done)
+ })
}
/// Official exercises: Dump the solution file form the binary and return its path.