Skip to content

Commit fd2bf9f

Browse files
committed
Simplify next_pending_exercise_ind
1 parent fc1f9f0 commit fd2bf9f

File tree

1 file changed

+16
-19
lines changed

1 file changed

+16
-19
lines changed

src/app_state.rs

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -313,25 +313,22 @@ impl AppState {
313313

314314
// Return the index of the next pending exercise or `None` if all exercises are done.
315315
fn next_pending_exercise_ind(&self) -> Option<usize> {
316-
if self.current_exercise_ind + 1 == self.exercises.len() {
317-
// The last exercise is done.
318-
// Search for exercises not done from the start.
319-
return self.exercises[..self.current_exercise_ind]
320-
.iter()
321-
.position(|exercise| !exercise.done);
322-
}
323-
324-
// The done exercise isn't the last one.
325-
// Search for a pending exercise after the current one and then from the start.
326-
match self.exercises[self.current_exercise_ind + 1..]
327-
.iter()
328-
.position(|exercise| !exercise.done)
329-
{
330-
Some(ind) => Some(self.current_exercise_ind + 1 + ind),
331-
None => self.exercises[..self.current_exercise_ind]
332-
.iter()
333-
.position(|exercise| !exercise.done),
334-
}
316+
let next_ind = self.current_exercise_ind + 1;
317+
self.exercises
318+
// If the exercise done isn't the last, search for pending exercises after it.
319+
.get(next_ind..)
320+
.and_then(|later_exercises| {
321+
later_exercises
322+
.iter()
323+
.position(|exercise| !exercise.done)
324+
.map(|ind| next_ind + ind)
325+
})
326+
// Search from the start.
327+
.or_else(|| {
328+
self.exercises[..self.current_exercise_ind]
329+
.iter()
330+
.position(|exercise| !exercise.done)
331+
})
335332
}
336333

337334
/// Official exercises: Dump the solution file form the binary and return its path.

0 commit comments

Comments
 (0)