mirror of
https://github.com/nushell/nushell
synced 2025-01-15 14:44:14 +00:00
Adding coerce filesize functionality to math avg median (#2848)
* Adding coerce filesize functionality to math avg median * Updating initial value creating in Math Summation Reducer * Update reducers.rs Co-authored-by: Jonathan Turner <jonathandturner@users.noreply.github.com>
This commit is contained in:
parent
b50cdd6de8
commit
eb62fd466e
2 changed files with 19 additions and 3 deletions
|
@ -128,8 +128,9 @@ fn compute_average(values: &[Value], name: impl Into<Tag>) -> Result<Value, Shel
|
||||||
&name,
|
&name,
|
||||||
)
|
)
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
let total_rows = UntaggedValue::decimal(number);
|
let total_rows = UntaggedValue::decimal(number);
|
||||||
let total = sum(UntaggedValue::int(0).into_untagged_value(), values.to_vec())?;
|
let total = sum(Value::nothing(), values.to_vec())?;
|
||||||
|
|
||||||
match total {
|
match total {
|
||||||
Value {
|
Value {
|
||||||
|
|
|
@ -41,16 +41,18 @@ pub fn reducer_for(
|
||||||
command: Reduce,
|
command: Reduce,
|
||||||
) -> Box<dyn Fn(Value, Vec<Value>) -> Result<Value, ShellError> + Send + Sync + 'static> {
|
) -> Box<dyn Fn(Value, Vec<Value>) -> Result<Value, ShellError> + Send + Sync + 'static> {
|
||||||
match command {
|
match command {
|
||||||
Reduce::Summation | Reduce::Default => Box::new(formula(
|
Reduce::Default => Box::new(formula(
|
||||||
UntaggedValue::int(0).into_untagged_value(),
|
UntaggedValue::int(0).into_untagged_value(),
|
||||||
Box::new(sum),
|
Box::new(sum),
|
||||||
)),
|
)),
|
||||||
|
Reduce::Summation => Box::new(|_, values| sum(values)),
|
||||||
Reduce::Minimum => Box::new(|_, values| min(values)),
|
Reduce::Minimum => Box::new(|_, values| min(values)),
|
||||||
Reduce::Maximum => Box::new(|_, values| max(values)),
|
Reduce::Maximum => Box::new(|_, values| max(values)),
|
||||||
Reduce::Product => Box::new(|_, values| product(values)),
|
Reduce::Product => Box::new(|_, values| product(values)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
pub enum Reduce {
|
pub enum Reduce {
|
||||||
Summation,
|
Summation,
|
||||||
Minimum,
|
Minimum,
|
||||||
|
@ -60,7 +62,20 @@ pub enum Reduce {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn sum(data: Vec<Value>) -> Result<Value, ShellError> {
|
pub fn sum(data: Vec<Value>) -> Result<Value, ShellError> {
|
||||||
let mut acc = UntaggedValue::int(0).into_untagged_value();
|
let first_value = data
|
||||||
|
.get(0)
|
||||||
|
.ok_or_else(|| ShellError::unexpected(ERR_EMPTY_DATA))?;
|
||||||
|
|
||||||
|
// Generate the initial accumulator value, of the correct type for
|
||||||
|
// the incoming data, this will be used in conjunction with the
|
||||||
|
// sum aggregator. Currently this is only handling, filesize,
|
||||||
|
// and other types are defaulting to an integer.
|
||||||
|
let mut acc = if first_value.is_filesize() {
|
||||||
|
UntaggedValue::filesize(0u64).into_untagged_value()
|
||||||
|
} else {
|
||||||
|
UntaggedValue::int(0).into_untagged_value()
|
||||||
|
};
|
||||||
|
|
||||||
for value in data {
|
for value in data {
|
||||||
match value.value {
|
match value.value {
|
||||||
UntaggedValue::Primitive(_) => {
|
UntaggedValue::Primitive(_) => {
|
||||||
|
|
Loading…
Reference in a new issue