1use crate::math::{
2 reducers::{Reduce, reducer_for},
3 utils::{run_with_function_with_cell_paths, run_with_function_with_cell_paths_const},
4};
5use nu_engine::command_prelude::*;
6
7#[derive(Clone)]
8pub struct MathSum;
9
10impl Command for MathSum {
11 fn name(&self) -> &str {
12 "math sum"
13 }
14
15 fn signature(&self) -> Signature {
16 Signature::build("math sum")
17 .input_output_types(vec![
18 (Type::List(Box::new(Type::Number)), Type::Number),
19 (Type::List(Box::new(Type::Duration)), Type::Duration),
20 (Type::List(Box::new(Type::Filesize)), Type::Filesize),
21 (Type::Range, Type::Number),
22 (Type::table(), Type::record()),
23 (Type::record(), Type::record()),
24 ])
25 .allow_variants_without_examples(true)
26 .rest(
27 "columns",
28 SyntaxShape::CellPath,
29 "The cell-paths/columns to operate on.",
30 )
31 .category(Category::Math)
32 }
33
34 fn description(&self) -> &str {
35 "Returns the sum of a list of numbers or of each column in a table."
36 }
37
38 fn search_terms(&self) -> Vec<&str> {
39 vec!["plus", "add", "total", "+"]
40 }
41
42 fn is_const(&self) -> bool {
43 true
44 }
45
46 fn run(
47 &self,
48 engine_state: &EngineState,
49 stack: &mut Stack,
50 call: &Call,
51 input: PipelineData,
52 ) -> Result<PipelineData, ShellError> {
53 run_with_function_with_cell_paths(engine_state, stack, call, input, summation)
54 }
55
56 fn run_const(
57 &self,
58 working_set: &StateWorkingSet,
59 call: &Call,
60 input: PipelineData,
61 ) -> Result<PipelineData, ShellError> {
62 run_with_function_with_cell_paths_const(working_set, call, input, summation)
63 }
64
65 fn examples(&self) -> Vec<Example<'_>> {
66 vec![
67 Example {
68 description: "Sum a list of numbers.",
69 example: "[1 2 3] | math sum",
70 result: Some(Value::test_int(6)),
71 },
72 Example {
73 description: "Get the disk usage for the current directory.",
74 example: "ls | get size | math sum",
75 result: None,
76 },
77 Example {
78 description: "Compute the sum of each column in a table.",
79 example: "[[a b]; [1 2] [3 4]] | math sum",
80 result: Some(Value::test_record(record! {
81 "a" => Value::test_int(4),
82 "b" => Value::test_int(6),
83 })),
84 },
85 Example {
86 description: "Sum the values of list-valued columns in a record.",
87 example: "{alice: [1 2 3], bob: [4 5 6]} | math sum",
88 result: Some(Value::test_record(record! {
89 "alice" => Value::test_int(6),
90 "bob" => Value::test_int(15),
91 })),
92 },
93 Example {
94 description: "Sum a single column using a cell path.",
95 example: "{alice: [1 2 3], bob: [4 5 6]} | math sum alice",
96 result: Some(Value::test_record(record! {
97 "alice" => Value::test_int(6),
98 "bob" => Value::list(
99 vec![Value::test_int(4), Value::test_int(5), Value::test_int(6)],
100 Span::test_data(),
101 ),
102 })),
103 },
104 ]
105 }
106}
107
108pub fn summation(values: &[Value], span: Span, head: Span) -> Result<Value, ShellError> {
109 let sum_func = reducer_for(Reduce::Summation);
110 sum_func(Value::nothing(head), values.to_vec(), span, head)
111}
112
113#[cfg(test)]
114mod test {
115 use super::*;
116
117 #[test]
118 fn test_examples() -> nu_test_support::Result {
119 nu_test_support::test().examples(MathSum)
120 }
121}