2021-11-01 21:29:34 +00: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};
|
Allow tables and records as input to math commands (#11496)
# Description
The math functions `avg`, `max`, `median`, `min`, `product`, `stddev`,
`sum` and `variance` all takes a list as input and return a number.
<https://github.com/nushell/nushell/blob/main/crates/nu-command/src/math/utils.rs>
contains code that makes these functions work for tables (by running the
function on each column), but this functionality has not been accessible
because the input types are too strict. This PR remedies this.
The functions should also work on records, since a record is basically a
one-row table.
Most of these functions also make sense for durations and file sizes,
except `product` of course. There's an implementation issue with
`stddev` and `variance` for durations and file sizes, but they could in
principle support it.
# User-Facing Changes
This PR only adds supported types, and doesn't remove any, so there
should be no breaking changes.
2024-01-17 12:39:50 +00:00
|
|
|
use nu_protocol::{
|
|
|
|
record, Category, Example, PipelineData, ShellError, Signature, Span, Type, Value,
|
|
|
|
};
|
2021-11-01 21:29:34 +00:00
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct SubCommand;
|
|
|
|
|
|
|
|
impl Command for SubCommand {
|
|
|
|
fn name(&self) -> &str {
|
|
|
|
"math product"
|
|
|
|
}
|
|
|
|
|
|
|
|
fn signature(&self) -> Signature {
|
2022-11-09 21:55:05 +00:00
|
|
|
Signature::build("math product")
|
Allow tables and records as input to math commands (#11496)
# Description
The math functions `avg`, `max`, `median`, `min`, `product`, `stddev`,
`sum` and `variance` all takes a list as input and return a number.
<https://github.com/nushell/nushell/blob/main/crates/nu-command/src/math/utils.rs>
contains code that makes these functions work for tables (by running the
function on each column), but this functionality has not been accessible
because the input types are too strict. This PR remedies this.
The functions should also work on records, since a record is basically a
one-row table.
Most of these functions also make sense for durations and file sizes,
except `product` of course. There's an implementation issue with
`stddev` and `variance` for durations and file sizes, but they could in
principle support it.
# User-Facing Changes
This PR only adds supported types, and doesn't remove any, so there
should be no breaking changes.
2024-01-17 12:39:50 +00:00
|
|
|
.input_output_types(vec![
|
|
|
|
(Type::List(Box::new(Type::Number)), Type::Number),
|
|
|
|
(Type::Range, Type::Number),
|
|
|
|
(Type::Table(vec![]), Type::Record(vec![])),
|
|
|
|
(Type::Record(vec![]), Type::Record(vec![])),
|
|
|
|
])
|
|
|
|
.allow_variants_without_examples(true)
|
2022-11-09 21:55:05 +00:00
|
|
|
.category(Category::Math)
|
2021-11-01 21:29:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn usage(&self) -> &str {
|
2023-03-01 05:33:02 +00:00
|
|
|
"Returns the product of a list of numbers or the products of each column of a table."
|
2021-11-01 21:29:34 +00:00
|
|
|
}
|
|
|
|
|
2022-04-18 21:33:32 +00:00
|
|
|
fn search_terms(&self) -> Vec<&str> {
|
|
|
|
vec!["times", "multiply", "x", "*"]
|
|
|
|
}
|
|
|
|
|
2021-11-01 21:29:34 +00:00
|
|
|
fn run(
|
|
|
|
&self,
|
|
|
|
_engine_state: &EngineState,
|
|
|
|
_stack: &mut Stack,
|
|
|
|
call: &Call,
|
|
|
|
input: PipelineData,
|
2023-02-05 21:17:46 +00:00
|
|
|
) -> Result<PipelineData, ShellError> {
|
2021-11-01 21:29:34 +00:00
|
|
|
run_with_function(call, input, product)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn examples(&self) -> Vec<Example> {
|
Allow tables and records as input to math commands (#11496)
# Description
The math functions `avg`, `max`, `median`, `min`, `product`, `stddev`,
`sum` and `variance` all takes a list as input and return a number.
<https://github.com/nushell/nushell/blob/main/crates/nu-command/src/math/utils.rs>
contains code that makes these functions work for tables (by running the
function on each column), but this functionality has not been accessible
because the input types are too strict. This PR remedies this.
The functions should also work on records, since a record is basically a
one-row table.
Most of these functions also make sense for durations and file sizes,
except `product` of course. There's an implementation issue with
`stddev` and `variance` for durations and file sizes, but they could in
principle support it.
# User-Facing Changes
This PR only adds supported types, and doesn't remove any, so there
should be no breaking changes.
2024-01-17 12:39:50 +00:00
|
|
|
vec![
|
|
|
|
Example {
|
|
|
|
description: "Compute the product of a list of numbers",
|
|
|
|
example: "[2 3 3 4] | math product",
|
|
|
|
result: Some(Value::test_int(72)),
|
|
|
|
},
|
|
|
|
Example {
|
|
|
|
description: "Compute the product of each column in a table",
|
|
|
|
example: "[[a b]; [1 2] [3 4]] | math product",
|
|
|
|
result: Some(Value::test_record(record! {
|
|
|
|
"a" => Value::test_int(3),
|
|
|
|
"b" => Value::test_int(8),
|
|
|
|
})),
|
|
|
|
},
|
|
|
|
]
|
2021-11-01 21:29:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Calculate product of given values
|
2023-07-31 19:47:46 +00:00
|
|
|
pub fn product(values: &[Value], span: Span, head: Span) -> Result<Value, ShellError> {
|
2021-11-01 21:29:34 +00:00
|
|
|
let product_func = reducer_for(Reduce::Product);
|
2023-07-31 19:47:46 +00:00
|
|
|
product_func(Value::nothing(head), values.to_vec(), span, head)
|
2021-11-01 21:29:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_examples() {
|
|
|
|
use crate::test_examples;
|
|
|
|
|
|
|
|
test_examples(SubCommand {})
|
|
|
|
}
|
|
|
|
}
|