1use crate::math::{
2 reducers::{Reduce, reducer_for},
3 utils::run_with_function,
4};
5use nu_engine::command_prelude::*;
6
7#[derive(Clone)]
8pub struct MathMax;
9
10impl Command for MathMax {
11 fn name(&self) -> &str {
12 "math max"
13 }
14
15 fn signature(&self) -> Signature {
16 Signature::build("math max")
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::List(Box::new(Type::Any)), Type::Any),
22 (Type::Range, Type::Number),
23 (Type::table(), Type::record()),
24 (Type::record(), Type::record()),
25 ])
26 .allow_variants_without_examples(true)
27 .category(Category::Math)
28 }
29
30 fn description(&self) -> &str {
31 "Returns the maximum of a list of values, or of columns in a table."
32 }
33
34 fn search_terms(&self) -> Vec<&str> {
35 vec!["maximum", "largest"]
36 }
37
38 fn is_const(&self) -> bool {
39 true
40 }
41
42 fn run(
43 &self,
44 _engine_state: &EngineState,
45 _stack: &mut Stack,
46 call: &Call,
47 input: PipelineData,
48 ) -> Result<PipelineData, ShellError> {
49 run_with_function(call, input, maximum)
50 }
51
52 fn run_const(
53 &self,
54 _working_set: &StateWorkingSet,
55 call: &Call,
56 input: PipelineData,
57 ) -> Result<PipelineData, ShellError> {
58 run_with_function(call, input, maximum)
59 }
60
61 fn examples(&self) -> Vec<Example> {
62 vec![
63 Example {
64 description: "Find the maximum of a list of numbers",
65 example: "[-50 100 25] | math max",
66 result: Some(Value::test_int(100)),
67 },
68 Example {
69 description: "Find the maxima of the columns of a table",
70 example: "[{a: 1 b: 3} {a: 2 b: -1}] | math max",
71 result: Some(Value::test_record(record! {
72 "a" => Value::test_int(2),
73 "b" => Value::test_int(3),
74 })),
75 },
76 Example {
77 description: "Find the maximum of a list of dates",
78 example: "[2022-02-02 2022-12-30 2012-12-12] | math max",
79 result: Some(Value::test_date(
80 "2022-12-30 00:00:00Z".parse().unwrap_or_default(),
81 )),
82 },
83 ]
84 }
85}
86
87pub fn maximum(values: &[Value], span: Span, head: Span) -> Result<Value, ShellError> {
88 let max_func = reducer_for(Reduce::Maximum);
89 max_func(Value::nothing(head), values.to_vec(), span, head)
90}
91
92#[cfg(test)]
93mod test {
94 use super::*;
95
96 #[test]
97 fn test_examples() {
98 use crate::test_examples;
99
100 test_examples(MathMax {})
101 }
102}