Reduced LOC by replacing several instances of Value::Int {}, Value::Float{}, Value::Bool {}, and Value::String {} with Value::int(), Value::float(), Value::boolean() and Value::string() (#7412)

# Description

While perusing Value.rs, I noticed the `Value::int()`, `Value::float()`,
`Value::boolean()` and `Value::string()` constructors, which seem
designed to make it easier to construct various Values, but which aren't
used often at all in the codebase. So, using a few find-replaces
regexes, I increased their usage. This reduces overall LOC because
structures like this:
```
Value::Int {
  val: a,
  span: head
}
```
are changed into
```
Value::int(a, head)
```
and are respected as such by the project's formatter.
There are little readability concerns because the second argument to all
of these is `span`, and it's almost always extremely obvious which is
the span at every callsite.

# User-Facing Changes

None.

# Tests + Formatting

Don't forget to add tests that cover your changes.

Make sure you've run and fixed any issues with these commands:

- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A
clippy::needless_collect` to check that you're using the standard code
style
- `cargo test --workspace` to check that all tests pass

# After Submitting

If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
This commit is contained in:
Leon
2022-12-10 02:37:51 +10:00
committed by GitHub
parent b56ad92e25
commit 220b105efb
139 changed files with 540 additions and 2141 deletions

View File

@@ -46,10 +46,7 @@ impl Command for SubCommand {
result: Some(Value::List {
vals: vec![
Value::test_int(50),
Value::Float {
val: 100.0,
span: Span::test_data(),
},
Value::float(100.0, Span::test_data()),
Value::test_int(25),
],
span: Span::test_data(),

View File

@@ -40,24 +40,14 @@ impl Command for SubCommand {
vec![Example {
description: "Compute the average of a list of numbers",
example: "[-50 100.0 25] | math avg",
result: Some(Value::Float {
val: 25.0,
span: Span::test_data(),
}),
result: Some(Value::float(25.0, Span::test_data())),
}]
}
}
pub fn average(values: &[Value], head: &Span) -> Result<Value, ShellError> {
let sum = reducer_for(Reduce::Summation);
let total = &sum(
Value::Int {
val: 0,
span: *head,
},
values.to_vec(),
*head,
)?;
let total = &sum(Value::int(0, *head), values.to_vec(), *head)?;
match total {
Value::Filesize { val, span } => Ok(Value::Filesize {
val: val / values.len() as i64,
@@ -67,14 +57,7 @@ pub fn average(values: &[Value], head: &Span) -> Result<Value, ShellError> {
val: val / values.len() as i64,
span: *span,
}),
_ => total.div(
*head,
&Value::Int {
val: values.len() as i64,
span: *head,
},
*head,
),
_ => total.div(*head, &Value::int(values.len() as i64, *head), *head),
}
}

View File

@@ -40,11 +40,7 @@ impl Command for SubCommand {
call: &Call,
_input: PipelineData,
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
Ok(Value::Float {
val: std::f64::consts::E,
span: call.head,
}
.into_pipeline_data())
Ok(Value::float(std::f64::consts::E, call.head).into_pipeline_data())
}
}

View File

@@ -47,10 +47,7 @@ impl Command for SubCommand {
vec![Example {
description: "Evaluate math in the pipeline",
example: "'10 / 4' | math eval",
result: Some(Value::Float {
val: 2.5,
span: Span::test_data(),
}),
result: Some(Value::float(2.5, Span::test_data())),
}]
}
}
@@ -104,10 +101,7 @@ pub fn parse(math_expression: &str, span: &Span) -> Result<Value, String> {
ctx.var("tau", std::f64::consts::TAU);
match meval::eval_str_with_context(math_expression, &ctx) {
Ok(num) if num.is_infinite() || num.is_nan() => Err("cannot represent result".to_string()),
Ok(num) => Ok(Value::Float {
val: num,
span: *span,
}),
Ok(num) => Ok(Value::float(num, *span)),
Err(error) => Err(error.to_string().to_lowercase()),
}
}

View File

@@ -46,10 +46,7 @@ impl Command for SubCommand {
Example {
description: "Compute the median of a list of numbers",
example: "[3 8 9 12 12 15] | math median",
result: Some(Value::Float {
val: 10.5,
span: Span::test_data(),
}),
result: Some(Value::float(10.5, Span::test_data())),
},
Example {
description: "Compute the medians of the columns of a table",

View File

@@ -171,14 +171,8 @@ pub fn mode(values: &[Value], head: &Span) -> Result<Value, ShellError> {
fn recreate_value(hashable_value: &HashableType, head: Span) -> Value {
let bytes = hashable_value.bytes;
match &hashable_value.original_type {
NumberTypes::Int => Value::Int {
val: i64::from_ne_bytes(bytes),
span: head,
},
NumberTypes::Float => Value::Float {
val: f64::from_ne_bytes(bytes),
span: head,
},
NumberTypes::Int => Value::int(i64::from_ne_bytes(bytes), head),
NumberTypes::Float => Value::float(f64::from_ne_bytes(bytes), head),
NumberTypes::Duration => Value::Duration {
val: i64::from_ne_bytes(bytes),
span: head,

View File

@@ -40,11 +40,7 @@ impl Command for SubCommand {
call: &Call,
_input: PipelineData,
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
Ok(Value::Float {
val: std::f64::consts::PI,
span: call.head,
}
.into_pipeline_data())
Ok(Value::float(std::f64::consts::PI, call.head).into_pipeline_data())
}
}

View File

@@ -80,10 +80,7 @@ pub fn sum(data: Vec<Value>, head: Span) -> Result<Value, ShellError> {
val: 0,
span: *span,
}),
Some(Value::Int { span, .. }) | Some(Value::Float { span, .. }) => Ok(Value::Int {
val: 0,
span: *span,
}),
Some(Value::Int { span, .. }) | Some(Value::Float { span, .. }) => Ok(Value::int(0, *span)),
None => Err(ShellError::UnsupportedInput(
"Empty input".to_string(),
head,
@@ -114,10 +111,7 @@ pub fn product(data: Vec<Value>, head: Span) -> Result<Value, ShellError> {
let initial_value = data.get(0);
let mut acc = match initial_value {
Some(Value::Int { span, .. }) | Some(Value::Float { span, .. }) => Ok(Value::Int {
val: 1,
span: *span,
}),
Some(Value::Int { span, .. }) | Some(Value::Float { span, .. }) => Ok(Value::int(1, *span)),
None => Err(ShellError::UnsupportedInput(
"Empty input".to_string(),
head,

View File

@@ -64,18 +64,9 @@ impl Command for SubCommand {
example: "[1.555 2.333 -3.111] | math round -p 2",
result: Some(Value::List {
vals: vec![
Value::Float {
val: 1.56,
span: Span::test_data(),
},
Value::Float {
val: 2.33,
span: Span::test_data(),
},
Value::Float {
val: -3.11,
span: Span::test_data(),
},
Value::float(1.56, Span::test_data()),
Value::float(2.33, Span::test_data()),
Value::float(-3.11, Span::test_data()),
],
span: Span::test_data(),
}),

View File

@@ -54,18 +54,12 @@ impl Command for SubCommand {
Example {
description: "Compute the standard deviation of a list of numbers",
example: "[1 2 3 4 5] | math stddev",
result: Some(Value::Float {
val: std::f64::consts::SQRT_2,
span: Span::test_data(),
}),
result: Some(Value::float(std::f64::consts::SQRT_2, Span::test_data())),
},
Example {
description: "Compute the sample standard deviation of a list of numbers",
example: "[1 2 3 4 5] | math stddev -s",
result: Some(Value::Float {
val: 1.5811388300841898,
span: Span::test_data(),
}),
result: Some(Value::float(1.5811388300841898, Span::test_data())),
},
]
}

View File

@@ -40,11 +40,7 @@ impl Command for SubCommand {
call: &Call,
_input: PipelineData,
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
Ok(Value::Float {
val: std::f64::consts::TAU,
span: call.head,
}
.into_pipeline_data())
Ok(Value::float(std::f64::consts::TAU, call.head).into_pipeline_data())
}
}

View File

@@ -46,36 +46,21 @@ impl Command for SubCommand {
Example {
description: "Get the variance of a list of numbers",
example: "echo [1 2 3 4 5] | math variance",
result: Some(Value::Float {
val: 2.0,
span: Span::test_data(),
}),
result: Some(Value::float(2.0, Span::test_data())),
},
Example {
description: "Get the sample variance of a list of numbers",
example: "[1 2 3 4 5] | math variance -s",
result: Some(Value::Float {
val: 2.5,
span: Span::test_data(),
}),
result: Some(Value::float(2.5, Span::test_data())),
},
]
}
}
fn sum_of_squares(values: &[Value], span: &Span) -> Result<Value, ShellError> {
let n = Value::Int {
val: values.len() as i64,
span: *span,
};
let mut sum_x = Value::Int {
val: 0,
span: *span,
};
let mut sum_x2 = Value::Int {
val: 0,
span: *span,
};
let n = Value::int(values.len() as i64, *span);
let mut sum_x = Value::int(0, *span);
let mut sum_x2 = Value::int(0, *span);
for value in values {
let v = match &value {
Value::Int { .. }
@@ -116,10 +101,7 @@ pub fn compute_variance(sample: bool) -> impl Fn(&[Value], &Span) -> Result<Valu
)),
other => other,
}?;
let n = Value::Int {
val: n as i64,
span: *span,
};
let n = Value::int(n as i64, *span);
ss.div(*span, &n, *span)
}
}