nu_command/math/
product.rs1use crate::math::{
2 reducers::{Reduce, reducer_for},
3 utils::run_with_function,
4};
5use nu_engine::command_prelude::*;
6
7#[derive(Clone)]
8pub struct MathProduct;
9
10impl Command for MathProduct {
11 fn name(&self) -> &str {
12 "math product"
13 }
14
15 fn signature(&self) -> Signature {
16 Signature::build("math product")
17 .input_output_types(vec![
18 (Type::List(Box::new(Type::Number)), Type::Number),
19 (Type::Range, Type::Number),
20 (Type::table(), Type::record()),
21 (Type::record(), Type::record()),
22 ])
23 .allow_variants_without_examples(true)
24 .category(Category::Math)
25 }
26
27 fn description(&self) -> &str {
28 "Returns the product of a list of numbers or the products of each column of a table."
29 }
30
31 fn search_terms(&self) -> Vec<&str> {
32 vec!["times", "multiply", "x", "*"]
33 }
34
35 fn is_const(&self) -> bool {
36 true
37 }
38
39 fn run(
40 &self,
41 _engine_state: &EngineState,
42 _stack: &mut Stack,
43 call: &Call,
44 input: PipelineData,
45 ) -> Result<PipelineData, ShellError> {
46 run_with_function(call, input, product)
47 }
48
49 fn run_const(
50 &self,
51 _working_set: &StateWorkingSet,
52 call: &Call,
53 input: PipelineData,
54 ) -> Result<PipelineData, ShellError> {
55 run_with_function(call, input, product)
56 }
57
58 fn examples(&self) -> Vec<Example<'_>> {
59 vec![
60 Example {
61 description: "Compute the product of a list of numbers",
62 example: "[2 3 3 4] | math product",
63 result: Some(Value::test_int(72)),
64 },
65 Example {
66 description: "Compute the product of each column in a table",
67 example: "[[a b]; [1 2] [3 4]] | math product",
68 result: Some(Value::test_record(record! {
69 "a" => Value::test_int(3),
70 "b" => Value::test_int(8),
71 })),
72 },
73 ]
74 }
75}
76
77pub fn product(values: &[Value], span: Span, head: Span) -> Result<Value, ShellError> {
79 let product_func = reducer_for(Reduce::Product);
80 product_func(Value::nothing(head), values.to_vec(), span, head)
81}
82
83#[cfg(test)]
84mod test {
85 use super::*;
86
87 #[test]
88 fn test_examples() {
89 use crate::test_examples;
90
91 test_examples(MathProduct {})
92 }
93}