removed unwraps (#430)

This commit is contained in:
Fernando Herrera
2021-12-04 12:38:21 +00:00
committed by GitHub
parent eed22605ef
commit 8a06ea133b
24 changed files with 233 additions and 159 deletions

View File

@ -1,3 +1,5 @@
use std::cmp::Ordering;
use crate::math::avg::average;
use crate::math::utils::run_with_function;
use nu_protocol::ast::Call;
@ -72,14 +74,14 @@ pub fn median(values: &[Value], head: &Span) -> Result<Value, ShellError> {
rhs_span: elem[1].span()?,
});
}
Ok(elem[0].partial_cmp(&elem[1]).unwrap())
Ok(elem[0].partial_cmp(&elem[1]).unwrap_or(Ordering::Equal))
})
.find(|elem| elem.is_err())
{
return Err(values);
}
sorted.sort_by(|a, b| a.partial_cmp(b).unwrap());
sorted.sort_by(|a, b| a.partial_cmp(b).unwrap_or(Ordering::Equal));
match take {
Pick::Median => {

View File

@ -78,7 +78,7 @@ pub fn mode(values: &[Value], head: &Span) -> Result<Value, ShellError> {
rhs_span: elem[1].span()?,
});
}
Ok(elem[0].partial_cmp(&elem[1]).unwrap())
Ok(elem[0].partial_cmp(&elem[1]).unwrap_or(Ordering::Equal))
})
.find(|elem| elem.is_err())
{
@ -87,7 +87,7 @@ pub fn mode(values: &[Value], head: &Span) -> Result<Value, ShellError> {
//In e-q, Value doesn't implement Hash or Eq, so we have to get the values inside
// But f64 doesn't implement Hash, so we get the binary representation to use as
// key in the HashMap
let hashable_values: Result<Vec<HashableType>, ShellError> = values
let hashable_values = values
.iter()
.map(|val| match val {
Value::Int { val, .. } => Ok(HashableType::new(val.to_ne_bytes(), NumberTypes::Int)),
@ -102,16 +102,13 @@ pub fn mode(values: &[Value], head: &Span) -> Result<Value, ShellError> {
}
other => Err(ShellError::UnsupportedInput(
"Unable to give a result with this input".to_string(),
other.span().unwrap(),
other.span()?,
)),
})
.collect::<Result<Vec<HashableType>, ShellError>>();
if let Err(not_hashable) = hashable_values {
return Err(not_hashable);
}
.collect::<Result<Vec<HashableType>, ShellError>>()?;
let mut frequency_map = std::collections::HashMap::new();
for v in hashable_values.unwrap() {
for v in hashable_values {
let counter = frequency_map.entry(v).or_insert(0);
*counter += 1;
}
@ -132,7 +129,7 @@ pub fn mode(values: &[Value], head: &Span) -> Result<Value, ShellError> {
}
}
modes.sort_by(|a, b| a.partial_cmp(b).unwrap());
modes.sort_by(|a, b| a.partial_cmp(b).unwrap_or(Ordering::Equal));
Ok(Value::List {
vals: modes,
span: *head,