Skip to content

Commit

Permalink
refactor: remove unwraps
Browse files Browse the repository at this point in the history
though unlikely too panic coz we only compute median on numbers, still cleaner...
  • Loading branch information
jqnatividad committed Dec 8, 2024
1 parent 4c5f28c commit a2c1597
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/unsorted.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,15 +114,15 @@ where
{
Some(match data.len() {
0 => return None,
1 => data.first()?.to_f64().unwrap(),
1 => data.first()?.to_f64()?,
len if len % 2 == 0 => {
let idx = len / 2;
let v1 = data.get(idx - 1)?.to_f64().unwrap();
let v2 = data.get(idx)?.to_f64().unwrap();
let v1 = data.get(idx - 1)?.to_f64()?;
let v2 = data.get(idx)?.to_f64()?;
(v1 + v2) / 2.0
}
// Safety: we know the index is within bounds
len => unsafe { data.get_unchecked(len / 2) }.to_f64().unwrap(),
len => unsafe { data.get_unchecked(len / 2) }.to_f64()?,
})
}

Expand Down

0 comments on commit a2c1597

Please sign in to comment.