nu_command/math/
min.rs

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 MathMin;
9
10impl Command for MathMin {
11    fn name(&self) -> &str {
12        "math min"
13    }
14
15    fn signature(&self) -> Signature {
16        Signature::build("math min")
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        "Finds the minimum within a list of values or tables."
32    }
33
34    fn search_terms(&self) -> Vec<&str> {
35        vec!["minimum", "smallest"]
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, minimum)
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, minimum)
59    }
60
61    fn examples(&self) -> Vec<Example> {
62        vec![
63            Example {
64                description: "Compute the minimum of a list of numbers",
65                example: "[-50 100 25] | math min",
66                result: Some(Value::test_int(-50)),
67            },
68            Example {
69                description: "Compute the minima of the columns of a table",
70                example: "[{a: 1 b: 3} {a: 2 b: -1}] | math min",
71                result: Some(Value::test_record(record! {
72                    "a" => Value::test_int(1),
73                    "b" => Value::test_int(-1),
74                })),
75            },
76            Example {
77                description: "Find the minimum of a list of arbitrary values (Warning: Weird)",
78                example: "[-50 'hello' true] | math min",
79                result: Some(Value::test_bool(true)),
80            },
81        ]
82    }
83}
84
85pub fn minimum(values: &[Value], span: Span, head: Span) -> Result<Value, ShellError> {
86    let min_func = reducer_for(Reduce::Minimum);
87    min_func(Value::nothing(head), values.to_vec(), span, head)
88}
89
90#[cfg(test)]
91mod test {
92    use super::*;
93
94    #[test]
95    fn test_examples() {
96        use crate::test_examples;
97
98        test_examples(MathMin {})
99    }
100}