mirror of
https://github.com/nushell/nushell.git
synced 2024-11-22 00:13:21 +01:00
add min and max
This commit is contained in:
parent
bc9a230e5e
commit
d3aa7eb7ef
@ -188,7 +188,7 @@ fn groups_to_table(
|
||||
record_map.insert("count".to_string(), Value::int(items.len() as i64, span));
|
||||
|
||||
if let Some(sum_col) = maybe_sum_column.clone() {
|
||||
match sum_celllpath(sum_col, &items, span) {
|
||||
match sum_celllpath(sum_col.clone(), &items, span) {
|
||||
Ok((sum_col_name, sum)) => {
|
||||
// add sum
|
||||
record_map
|
||||
@ -206,6 +206,20 @@ fn groups_to_table(
|
||||
record_map.insert("error".to_string(), Value::error(err, span));
|
||||
}
|
||||
}
|
||||
|
||||
match minmax_celllpath(sum_col, &items, span) {
|
||||
Ok((min_col_name, min, max)) => {
|
||||
// add min
|
||||
record_map
|
||||
.insert(min_col_name.clone() + "_min", Value::float(min, span));
|
||||
// add max
|
||||
record_map.insert(min_col_name + "_max", Value::float(max, span));
|
||||
}
|
||||
Err(err) => {
|
||||
// It seems a little odd to be adding an error to the record
|
||||
record_map.insert("error".to_string(), Value::error(err, span));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Value::record(record_map, span)
|
||||
@ -223,7 +237,7 @@ fn sum_celllpath(column: Value, items: &[Value], span: Span) -> Result<(String,
|
||||
v.clone()
|
||||
.follow_cell_path(&val.members, false)
|
||||
.unwrap_or_else(|_| Value::float(0.0, span))
|
||||
.as_float()
|
||||
.coerce_float()
|
||||
.unwrap_or(0.0)
|
||||
})
|
||||
.sum();
|
||||
@ -236,6 +250,42 @@ fn sum_celllpath(column: Value, items: &[Value], span: Span) -> Result<(String,
|
||||
}
|
||||
}
|
||||
|
||||
fn minmax_celllpath(
|
||||
column: Value,
|
||||
items: &[Value],
|
||||
span: Span,
|
||||
) -> Result<(String, f64, f64), ShellError> {
|
||||
if let Value::CellPath { val, .. } = column {
|
||||
let collection = items
|
||||
.iter()
|
||||
.map(|v| {
|
||||
v.clone()
|
||||
.follow_cell_path(&val.members, false)
|
||||
.unwrap_or_else(|_| Value::float(0.0, span))
|
||||
.coerce_float()
|
||||
.unwrap_or(0.0)
|
||||
})
|
||||
.collect::<Vec<f64>>();
|
||||
|
||||
Ok((
|
||||
val.to_column_name(),
|
||||
*collection
|
||||
.iter()
|
||||
.min_by(|a, b| a.total_cmp(b))
|
||||
.unwrap_or(&0.0),
|
||||
*collection
|
||||
.iter()
|
||||
.max_by(|a, b| a.total_cmp(b))
|
||||
.unwrap_or(&0.0),
|
||||
))
|
||||
} else {
|
||||
Err(ShellError::TypeMismatch {
|
||||
err_message: format!("Only CellPath's are allowed. Found {}.", column.get_type()),
|
||||
span,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::*;
|
||||
|
Loading…
Reference in New Issue
Block a user