2021-10-27 03:00:50 +02:00
|
|
|
use crate::math::reducers::{reducer_for, Reduce};
|
|
|
|
use crate::math::utils::run_with_function;
|
|
|
|
use nu_protocol::ast::Call;
|
|
|
|
use nu_protocol::engine::{Command, EngineState, Stack};
|
2022-11-09 22:55:05 +01:00
|
|
|
use nu_protocol::{Category, Example, PipelineData, ShellError, Signature, Span, Type, Value};
|
2021-10-27 03:00:50 +02:00
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct SubCommand;
|
|
|
|
|
|
|
|
impl Command for SubCommand {
|
|
|
|
fn name(&self) -> &str {
|
|
|
|
"math avg"
|
|
|
|
}
|
|
|
|
|
|
|
|
fn signature(&self) -> Signature {
|
2022-11-09 22:55:05 +01:00
|
|
|
Signature::build("math avg")
|
2023-07-14 05:20:35 +02:00
|
|
|
.input_output_types(vec![
|
|
|
|
(Type::List(Box::new(Type::Number)), Type::Number),
|
|
|
|
(Type::List(Box::new(Type::Duration)), Type::Duration),
|
|
|
|
(Type::List(Box::new(Type::Filesize)), Type::Filesize),
|
|
|
|
])
|
|
|
|
.allow_variants_without_examples(true)
|
2022-11-09 22:55:05 +01:00
|
|
|
.category(Category::Math)
|
2021-10-27 03:00:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn usage(&self) -> &str {
|
2023-03-01 06:33:02 +01:00
|
|
|
"Returns the average of a list of numbers."
|
2021-10-27 03:00:50 +02:00
|
|
|
}
|
|
|
|
|
2022-04-18 23:33:32 +02:00
|
|
|
fn search_terms(&self) -> Vec<&str> {
|
2022-08-24 11:16:47 +02:00
|
|
|
vec!["average", "mean", "statistics"]
|
2022-04-18 23:33:32 +02:00
|
|
|
}
|
|
|
|
|
2021-10-27 03:00:50 +02:00
|
|
|
fn run(
|
|
|
|
&self,
|
|
|
|
_engine_state: &EngineState,
|
|
|
|
_stack: &mut Stack,
|
|
|
|
call: &Call,
|
|
|
|
input: PipelineData,
|
2023-02-05 22:17:46 +01:00
|
|
|
) -> Result<PipelineData, ShellError> {
|
2021-10-27 03:00:50 +02:00
|
|
|
run_with_function(call, input, average)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn examples(&self) -> Vec<Example> {
|
|
|
|
vec![Example {
|
2022-11-09 22:55:05 +01:00
|
|
|
description: "Compute the average of a list of numbers",
|
2021-10-27 03:00:50 +02:00
|
|
|
example: "[-50 100.0 25] | math avg",
|
2022-12-24 14:41:57 +01:00
|
|
|
result: Some(Value::test_float(25.0)),
|
2021-10-27 03:00:50 +02:00
|
|
|
}]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-31 21:47:46 +02:00
|
|
|
pub fn average(values: &[Value], span: Span, head: Span) -> Result<Value, ShellError> {
|
2021-10-27 03:00:50 +02:00
|
|
|
let sum = reducer_for(Reduce::Summation);
|
2023-07-31 21:47:46 +02:00
|
|
|
let total = &sum(Value::int(0, head), values.to_vec(), span, head)?;
|
2023-09-03 16:27:29 +02:00
|
|
|
let span = total.span();
|
2021-10-27 03:00:50 +02:00
|
|
|
match total {
|
2023-09-03 16:27:29 +02:00
|
|
|
Value::Filesize { val, .. } => Ok(Value::filesize(val / values.len() as i64, span)),
|
|
|
|
Value::Duration { val, .. } => Ok(Value::duration(val / values.len() as i64, span)),
|
2023-07-31 21:47:46 +02:00
|
|
|
_ => total.div(head, &Value::int(values.len() as i64, head), head),
|
2021-10-27 03:00:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_examples() {
|
|
|
|
use crate::test_examples;
|
|
|
|
|
|
|
|
test_examples(SubCommand {})
|
|
|
|
}
|
|
|
|
}
|