2021-10-29 19:02:42 +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};
|
Create `Record` type (#10103)
# Description
This PR creates a new `Record` type to reduce duplicate code and
possibly bugs as well. (This is an edited version of #9648.)
- `Record` implements `FromIterator` and `IntoIterator` and so can be
iterated over or collected into. For example, this helps with
conversions to and from (hash)maps. (Also, no more
`cols.iter().zip(vals)`!)
- `Record` has a `push(col, val)` function to help insure that the
number of columns is equal to the number of values. I caught a few
potential bugs thanks to this (e.g. in the `ls` command).
- Finally, this PR also adds a `record!` macro that helps simplify
record creation. It is used like so:
```rust
record! {
"key1" => some_value,
"key2" => Value::string("text", span),
"key3" => Value::int(optional_int.unwrap_or(0), span),
"key4" => Value::bool(config.setting, span),
}
```
Since macros hinder formatting, etc., the right hand side values should
be relatively short and sweet like the examples above.
Where possible, prefer `record!` or `.collect()` on an iterator instead
of multiple `Record::push`s, since the first two automatically set the
record capacity and do less work overall.
# User-Facing Changes
Besides the changes in `nu-protocol` the only other breaking changes are
to `nu-table::{ExpandedTable::build_map, JustTable::kv_table}`.
2023-08-24 19:50:29 +00:00
|
|
|
use nu_protocol::{
|
2023-10-28 12:52:31 +00:00
|
|
|
record, Category, Example, PipelineData, ShellError, Signature, Span, Type, Value,
|
Create `Record` type (#10103)
# Description
This PR creates a new `Record` type to reduce duplicate code and
possibly bugs as well. (This is an edited version of #9648.)
- `Record` implements `FromIterator` and `IntoIterator` and so can be
iterated over or collected into. For example, this helps with
conversions to and from (hash)maps. (Also, no more
`cols.iter().zip(vals)`!)
- `Record` has a `push(col, val)` function to help insure that the
number of columns is equal to the number of values. I caught a few
potential bugs thanks to this (e.g. in the `ls` command).
- Finally, this PR also adds a `record!` macro that helps simplify
record creation. It is used like so:
```rust
record! {
"key1" => some_value,
"key2" => Value::string("text", span),
"key3" => Value::int(optional_int.unwrap_or(0), span),
"key4" => Value::bool(config.setting, span),
}
```
Since macros hinder formatting, etc., the right hand side values should
be relatively short and sweet like the examples above.
Where possible, prefer `record!` or `.collect()` on an iterator instead
of multiple `Record::push`s, since the first two automatically set the
record capacity and do less work overall.
# User-Facing Changes
Besides the changes in `nu-protocol` the only other breaking changes are
to `nu-table::{ExpandedTable::build_map, JustTable::kv_table}`.
2023-08-24 19:50:29 +00:00
|
|
|
};
|
2021-10-29 19:02:42 +00:00
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct SubCommand;
|
|
|
|
|
|
|
|
impl Command for SubCommand {
|
|
|
|
fn name(&self) -> &str {
|
|
|
|
"math max"
|
|
|
|
}
|
|
|
|
|
|
|
|
fn signature(&self) -> Signature {
|
2022-11-09 21:55:05 +00:00
|
|
|
Signature::build("math max")
|
|
|
|
.input_output_types(vec![
|
2023-07-28 20:12:58 +00:00
|
|
|
(Type::List(Box::new(Type::Any)), Type::Any),
|
2022-11-09 21:55:05 +00:00
|
|
|
(Type::Table(vec![]), Type::Record(vec![])),
|
|
|
|
])
|
2023-07-14 03:20:35 +00:00
|
|
|
.allow_variants_without_examples(true)
|
2022-11-09 21:55:05 +00:00
|
|
|
.category(Category::Math)
|
2021-10-29 19:02:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn usage(&self) -> &str {
|
2023-07-28 20:12:58 +00:00
|
|
|
"Returns the maximum of a list of values, or of columns in a table."
|
2021-10-29 19:02:42 +00:00
|
|
|
}
|
|
|
|
|
2022-04-18 21:33:32 +00:00
|
|
|
fn search_terms(&self) -> Vec<&str> {
|
|
|
|
vec!["maximum", "largest"]
|
|
|
|
}
|
|
|
|
|
2021-10-29 19:02:42 +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-10-29 19:02:42 +00:00
|
|
|
run_with_function(call, input, maximum)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn examples(&self) -> Vec<Example> {
|
2022-11-09 21:55:05 +00:00
|
|
|
vec![
|
|
|
|
Example {
|
|
|
|
description: "Find the maximum of list of numbers",
|
|
|
|
example: "[-50 100 25] | math max",
|
|
|
|
result: Some(Value::test_int(100)),
|
|
|
|
},
|
|
|
|
Example {
|
|
|
|
description: "Find the maxima of the columns of a table",
|
|
|
|
example: "[{a: 1 b: 3} {a: 2 b: -1}] | math max",
|
2023-10-28 12:52:31 +00:00
|
|
|
result: Some(Value::test_record(record! {
|
|
|
|
"a" => Value::test_int(2),
|
|
|
|
"b" => Value::test_int(3),
|
Create `Record` type (#10103)
# Description
This PR creates a new `Record` type to reduce duplicate code and
possibly bugs as well. (This is an edited version of #9648.)
- `Record` implements `FromIterator` and `IntoIterator` and so can be
iterated over or collected into. For example, this helps with
conversions to and from (hash)maps. (Also, no more
`cols.iter().zip(vals)`!)
- `Record` has a `push(col, val)` function to help insure that the
number of columns is equal to the number of values. I caught a few
potential bugs thanks to this (e.g. in the `ls` command).
- Finally, this PR also adds a `record!` macro that helps simplify
record creation. It is used like so:
```rust
record! {
"key1" => some_value,
"key2" => Value::string("text", span),
"key3" => Value::int(optional_int.unwrap_or(0), span),
"key4" => Value::bool(config.setting, span),
}
```
Since macros hinder formatting, etc., the right hand side values should
be relatively short and sweet like the examples above.
Where possible, prefer `record!` or `.collect()` on an iterator instead
of multiple `Record::push`s, since the first two automatically set the
record capacity and do less work overall.
# User-Facing Changes
Besides the changes in `nu-protocol` the only other breaking changes are
to `nu-table::{ExpandedTable::build_map, JustTable::kv_table}`.
2023-08-24 19:50:29 +00:00
|
|
|
})),
|
2022-11-09 21:55:05 +00:00
|
|
|
},
|
|
|
|
]
|
2021-10-29 19:02:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-31 19:47:46 +00:00
|
|
|
pub fn maximum(values: &[Value], span: Span, head: Span) -> Result<Value, ShellError> {
|
2021-10-29 19:02:42 +00:00
|
|
|
let max_func = reducer_for(Reduce::Maximum);
|
2023-07-31 19:47:46 +00:00
|
|
|
max_func(Value::nothing(head), values.to_vec(), span, head)
|
2021-10-29 19:02:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_examples() {
|
|
|
|
use crate::test_examples;
|
|
|
|
|
|
|
|
test_examples(SubCommand {})
|
|
|
|
}
|
|
|
|
}
|