Skip to content

Commit 58a25fb

Browse files
Philippe-Choletjswrenn
authored andcommitted
Use try_fold in CompleteState::remaining
1 parent bef0014 commit 58a25fb

File tree

1 file changed

+8
-12
lines changed

1 file changed

+8
-12
lines changed

src/permutations.rs

+8-12
Original file line numberDiff line numberDiff line change
@@ -240,20 +240,16 @@ impl CompleteState {
240240
if n < k {
241241
return Some(0);
242242
}
243-
(n - k + 1..=n).fold(Some(1), |acc, i| {
244-
acc.and_then(|acc| acc.checked_mul(i))
245-
})
243+
(n - k + 1..=n).try_fold(1usize, |acc, i| acc.checked_mul(i))
246244
}
247245
CompleteState::Ongoing { ref indices, ref cycles } => {
248-
let mut count: usize = 0;
249-
250-
for (i, &c) in cycles.iter().enumerate() {
251-
let radix = indices.len() - i;
252-
count = count.checked_mul(radix)
253-
.and_then(|count| count.checked_add(c))?;
254-
}
255-
256-
Some(count)
246+
cycles
247+
.iter()
248+
.enumerate()
249+
.try_fold(0usize, |acc, (i, &c)| {
250+
acc.checked_mul(indices.len() - i)
251+
.and_then(|count| count.checked_add(c))
252+
})
257253
}
258254
}
259255
}

0 commit comments

Comments
 (0)