mirror of
https://github.com/nushell/nushell.git
synced 2024-11-22 16:33:37 +01:00
Remove math eval
command (#7284)
Reasoning: Most missing math commands are implemented with #7258. The `meval` crate itself declares that it doesn't strive to stringent standards (https://docs.rs/meval/latest/meval/#related-projects). For example no particular special casing or transformations are performed to ensure numerical stability. It uses the same rust `std` library functions we use or have access to (and `f64`). While the command call syntax in nushell may be a bit more verbose, having a single source of truth and common commands is beneficial. Furthermore the `math` commands can themselves implement broadcasting over lists (or table columns). Closes #7073 Removed dependencies: - `meval` - `nom 1.2.4` (duplicate) User-Facing Changes: Scripts using `math eval` will break. We remove a further `eval` like behavior to get results through runtime evaluation (albeit limited in scope) Tests: - Updated tests that internally used `math eval`. - Removed one test that primarily used `math eval` to obtain a result from `str join`
This commit is contained in:
parent
429127793f
commit
9bc4e6794d
27
Cargo.lock
generated
27
Cargo.lock
generated
@ -104,7 +104,7 @@ version = "0.2.0"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "220044e6a1bb31ddee4e3db724d29767f352de47445a6cd75e1a173142136c83"
|
checksum = "220044e6a1bb31ddee4e3db724d29767f352de47445a6cd75e1a173142136c83"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"nom 7.1.1",
|
"nom",
|
||||||
"vte",
|
"vte",
|
||||||
]
|
]
|
||||||
|
|
||||||
@ -479,7 +479,7 @@ version = "0.6.0"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "6fac387a98bb7c37292057cffc56d62ecb629900026402633ae9160df93a8766"
|
checksum = "6fac387a98bb7c37292057cffc56d62ecb629900026402633ae9160df93a8766"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"nom 7.1.1",
|
"nom",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@ -2225,16 +2225,6 @@ dependencies = [
|
|||||||
"autocfg",
|
"autocfg",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "meval"
|
|
||||||
version = "0.2.0"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "f79496a5651c8d57cd033c5add8ca7ee4e3d5f7587a4777484640d9cb60392d9"
|
|
||||||
dependencies = [
|
|
||||||
"fnv",
|
|
||||||
"nom 1.2.4",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "miette"
|
name = "miette"
|
||||||
version = "5.3.0"
|
version = "5.3.0"
|
||||||
@ -2439,12 +2429,6 @@ version = "0.1.14"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "72ef4a56884ca558e5ddb05a1d1e7e1bfd9a68d9ed024c21704cc98872dae1bb"
|
checksum = "72ef4a56884ca558e5ddb05a1d1e7e1bfd9a68d9ed024c21704cc98872dae1bb"
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "nom"
|
|
||||||
version = "1.2.4"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "a5b8c256fd9471521bcb84c3cdba98921497f1a331cbc15b8030fc63b82050ce"
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "nom"
|
name = "nom"
|
||||||
version = "7.1.1"
|
version = "7.1.1"
|
||||||
@ -2465,7 +2449,7 @@ dependencies = [
|
|||||||
"indent_write",
|
"indent_write",
|
||||||
"joinery",
|
"joinery",
|
||||||
"memchr",
|
"memchr",
|
||||||
"nom 7.1.1",
|
"nom",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@ -2628,7 +2612,6 @@ dependencies = [
|
|||||||
"log",
|
"log",
|
||||||
"lscolors",
|
"lscolors",
|
||||||
"md-5",
|
"md-5",
|
||||||
"meval",
|
|
||||||
"mime",
|
"mime",
|
||||||
"mime_guess",
|
"mime_guess",
|
||||||
"notify",
|
"notify",
|
||||||
@ -3672,7 +3655,7 @@ version = "0.0.0"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "a4a63d338dec139f56dacc692ca63ad35a6be6a797442479b55acd611d79e906"
|
checksum = "a4a63d338dec139f56dacc692ca63ad35a6be6a797442479b55acd611d79e906"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"nom 7.1.1",
|
"nom",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@ -5574,7 +5557,7 @@ dependencies = [
|
|||||||
"bstr",
|
"bstr",
|
||||||
"const_format",
|
"const_format",
|
||||||
"itertools",
|
"itertools",
|
||||||
"nom 7.1.1",
|
"nom",
|
||||||
"nom-supreme",
|
"nom-supreme",
|
||||||
"pori",
|
"pori",
|
||||||
"regex",
|
"regex",
|
||||||
|
@ -57,7 +57,6 @@ itertools = "0.10.0"
|
|||||||
log = "0.4.14"
|
log = "0.4.14"
|
||||||
lscolors = { version = "0.12.0", features = ["crossterm"], default-features = false }
|
lscolors = { version = "0.12.0", features = ["crossterm"], default-features = false }
|
||||||
md5 = { package = "md-5", version = "0.10.0" }
|
md5 = { package = "md-5", version = "0.10.0" }
|
||||||
meval = "0.2.0"
|
|
||||||
mime = "0.3.16"
|
mime = "0.3.16"
|
||||||
mime_guess = "2.0.4"
|
mime_guess = "2.0.4"
|
||||||
notify = "4.0.17"
|
notify = "4.0.17"
|
||||||
|
@ -401,7 +401,6 @@ pub fn create_default_context() -> EngineState {
|
|||||||
MathAbs,
|
MathAbs,
|
||||||
MathAvg,
|
MathAvg,
|
||||||
MathCeil,
|
MathCeil,
|
||||||
MathEval,
|
|
||||||
MathFloor,
|
MathFloor,
|
||||||
MathMax,
|
MathMax,
|
||||||
MathMedian,
|
MathMedian,
|
||||||
@ -481,6 +480,7 @@ pub fn create_default_context() -> EngineState {
|
|||||||
StrDecimalDeprecated,
|
StrDecimalDeprecated,
|
||||||
StrIntDeprecated,
|
StrIntDeprecated,
|
||||||
StrFindReplaceDeprecated,
|
StrFindReplaceDeprecated,
|
||||||
|
MathEvalDeprecated,
|
||||||
};
|
};
|
||||||
|
|
||||||
#[cfg(feature = "plugin")]
|
#[cfg(feature = "plugin")]
|
||||||
|
40
crates/nu-command/src/deprecated/math_eval.rs
Normal file
40
crates/nu-command/src/deprecated/math_eval.rs
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
use nu_protocol::ast::Call;
|
||||||
|
use nu_protocol::engine::{Command, EngineState, Stack};
|
||||||
|
use nu_protocol::{Category, PipelineData, ShellError, Signature, SyntaxShape};
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
|
pub struct SubCommand;
|
||||||
|
|
||||||
|
impl Command for SubCommand {
|
||||||
|
fn name(&self) -> &str {
|
||||||
|
"math eval"
|
||||||
|
}
|
||||||
|
|
||||||
|
fn signature(&self) -> Signature {
|
||||||
|
Signature::build("math eval")
|
||||||
|
.rest(
|
||||||
|
"ignored",
|
||||||
|
SyntaxShape::Any,
|
||||||
|
"arguments to deprecated command are ignored",
|
||||||
|
)
|
||||||
|
.category(Category::Deprecated)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn usage(&self) -> &str {
|
||||||
|
"Deprecated command"
|
||||||
|
}
|
||||||
|
|
||||||
|
fn run(
|
||||||
|
&self,
|
||||||
|
_engine_state: &EngineState,
|
||||||
|
_stack: &mut Stack,
|
||||||
|
call: &Call,
|
||||||
|
_input: PipelineData,
|
||||||
|
) -> Result<PipelineData, ShellError> {
|
||||||
|
Err(ShellError::DeprecatedCommand(
|
||||||
|
self.name().to_string(),
|
||||||
|
"math <subcommands>".to_string(),
|
||||||
|
call.head,
|
||||||
|
))
|
||||||
|
}
|
||||||
|
}
|
@ -1,5 +1,6 @@
|
|||||||
mod deprecated_commands;
|
mod deprecated_commands;
|
||||||
mod hash_base64;
|
mod hash_base64;
|
||||||
|
mod math_eval;
|
||||||
mod source;
|
mod source;
|
||||||
mod str_datetime;
|
mod str_datetime;
|
||||||
mod str_decimal;
|
mod str_decimal;
|
||||||
@ -8,6 +9,7 @@ mod str_int;
|
|||||||
|
|
||||||
pub use deprecated_commands::*;
|
pub use deprecated_commands::*;
|
||||||
pub use hash_base64::HashBase64;
|
pub use hash_base64::HashBase64;
|
||||||
|
pub use math_eval::SubCommand as MathEvalDeprecated;
|
||||||
pub use source::Source;
|
pub use source::Source;
|
||||||
pub use str_datetime::StrDatetimeDeprecated;
|
pub use str_datetime::StrDatetimeDeprecated;
|
||||||
pub use str_decimal::StrDecimalDeprecated;
|
pub use str_decimal::StrDecimalDeprecated;
|
||||||
|
@ -1,115 +0,0 @@
|
|||||||
use nu_engine::CallExt;
|
|
||||||
use nu_protocol::ast::Call;
|
|
||||||
use nu_protocol::engine::{Command, EngineState, Stack};
|
|
||||||
use nu_protocol::{
|
|
||||||
Category, Example, PipelineData, ShellError, Signature, Span, Spanned, SyntaxShape, Type, Value,
|
|
||||||
};
|
|
||||||
|
|
||||||
#[derive(Clone)]
|
|
||||||
pub struct SubCommand;
|
|
||||||
|
|
||||||
impl Command for SubCommand {
|
|
||||||
fn name(&self) -> &str {
|
|
||||||
"math eval"
|
|
||||||
}
|
|
||||||
|
|
||||||
fn usage(&self) -> &str {
|
|
||||||
"Evaluate a math expression into a number"
|
|
||||||
}
|
|
||||||
|
|
||||||
fn search_terms(&self) -> Vec<&str> {
|
|
||||||
vec!["evaluation", "solve", "equation", "expression"]
|
|
||||||
}
|
|
||||||
|
|
||||||
fn signature(&self) -> Signature {
|
|
||||||
Signature::build("math eval")
|
|
||||||
.input_output_types(vec![(Type::String, Type::Number)])
|
|
||||||
.optional(
|
|
||||||
"math expression",
|
|
||||||
SyntaxShape::String,
|
|
||||||
"the math expression to evaluate",
|
|
||||||
)
|
|
||||||
.category(Category::Math)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn run(
|
|
||||||
&self,
|
|
||||||
engine_state: &EngineState,
|
|
||||||
stack: &mut Stack,
|
|
||||||
call: &Call,
|
|
||||||
input: PipelineData,
|
|
||||||
) -> Result<PipelineData, ShellError> {
|
|
||||||
let spanned_expr: Option<Spanned<String>> = call.opt(engine_state, stack, 0)?;
|
|
||||||
eval(spanned_expr, input, engine_state, call.head)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn examples(&self) -> Vec<Example> {
|
|
||||||
vec![Example {
|
|
||||||
description: "Evaluate math in the pipeline",
|
|
||||||
example: "'10 / 4' | math eval",
|
|
||||||
result: Some(Value::test_float(2.5)),
|
|
||||||
}]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn eval(
|
|
||||||
spanned_expr: Option<Spanned<String>>,
|
|
||||||
input: PipelineData,
|
|
||||||
engine_state: &EngineState,
|
|
||||||
head: Span,
|
|
||||||
) -> Result<PipelineData, ShellError> {
|
|
||||||
if let Some(expr) = spanned_expr {
|
|
||||||
match parse(&expr.item, &expr.span) {
|
|
||||||
Ok(value) => Ok(PipelineData::Value(value, None)),
|
|
||||||
Err(err) => Err(ShellError::UnsupportedInput(
|
|
||||||
format!("Math evaluation error: {}", err),
|
|
||||||
"value originates from here".into(),
|
|
||||||
head,
|
|
||||||
expr.span,
|
|
||||||
)),
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if let PipelineData::Value(Value::Nothing { .. }, ..) = input {
|
|
||||||
return Ok(input);
|
|
||||||
}
|
|
||||||
input.map(
|
|
||||||
move |val| match val.as_string() {
|
|
||||||
Ok(string) => match parse(&string, &val.span().unwrap_or(head)) {
|
|
||||||
Ok(value) => value,
|
|
||||||
Err(err) => Value::Error {
|
|
||||||
error: ShellError::UnsupportedInput(
|
|
||||||
format!("Math evaluation error: {}", err),
|
|
||||||
"value originates from here".into(),
|
|
||||||
head,
|
|
||||||
val.expect_span(),
|
|
||||||
),
|
|
||||||
},
|
|
||||||
},
|
|
||||||
Err(error) => Value::Error { error },
|
|
||||||
},
|
|
||||||
engine_state.ctrlc.clone(),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn parse(math_expression: &str, span: &Span) -> Result<Value, String> {
|
|
||||||
let mut ctx = meval::Context::new();
|
|
||||||
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(num, *span)),
|
|
||||||
Err(error) => Err(error.to_string().to_lowercase()),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
mod test {
|
|
||||||
use super::*;
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_examples() {
|
|
||||||
use crate::test_examples;
|
|
||||||
|
|
||||||
test_examples(SubCommand {})
|
|
||||||
}
|
|
||||||
}
|
|
@ -10,7 +10,6 @@ mod ceil;
|
|||||||
mod cos;
|
mod cos;
|
||||||
mod cosh;
|
mod cosh;
|
||||||
mod euler;
|
mod euler;
|
||||||
mod eval;
|
|
||||||
mod floor;
|
mod floor;
|
||||||
mod ln;
|
mod ln;
|
||||||
mod log;
|
mod log;
|
||||||
@ -37,7 +36,6 @@ mod variance;
|
|||||||
pub use abs::SubCommand as MathAbs;
|
pub use abs::SubCommand as MathAbs;
|
||||||
pub use avg::SubCommand as MathAvg;
|
pub use avg::SubCommand as MathAvg;
|
||||||
pub use ceil::SubCommand as MathCeil;
|
pub use ceil::SubCommand as MathCeil;
|
||||||
pub use eval::SubCommand as MathEval;
|
|
||||||
pub use floor::SubCommand as MathFloor;
|
pub use floor::SubCommand as MathFloor;
|
||||||
pub use math_::MathCommand as Math;
|
pub use math_::MathCommand as Math;
|
||||||
pub use max::SubCommand as MathMax;
|
pub use max::SubCommand as MathMax;
|
||||||
|
@ -1,85 +0,0 @@
|
|||||||
use nu_test_support::{nu, pipeline};
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn evaluates_two_plus_two() {
|
|
||||||
let actual = nu!(
|
|
||||||
cwd: ".", pipeline(
|
|
||||||
r#"
|
|
||||||
math eval "2 + 2"
|
|
||||||
"#
|
|
||||||
));
|
|
||||||
|
|
||||||
assert!(actual.out.contains('4'));
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn evaluates_two_to_the_power_four() {
|
|
||||||
let actual = nu!(
|
|
||||||
cwd: ".", pipeline(
|
|
||||||
r#"
|
|
||||||
echo "2 ^ 4" | math eval
|
|
||||||
"#
|
|
||||||
));
|
|
||||||
|
|
||||||
assert!(actual.out.contains("16"));
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn evaluates_three_multiplied_by_five() {
|
|
||||||
let actual = nu!(
|
|
||||||
cwd: ".", pipeline(
|
|
||||||
r#"
|
|
||||||
echo "3 * 5" | math eval
|
|
||||||
"#
|
|
||||||
));
|
|
||||||
|
|
||||||
assert!(actual.out.contains("15"));
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn evaluates_twenty_four_divided_by_two() {
|
|
||||||
let actual = nu!(
|
|
||||||
cwd: ".", pipeline(
|
|
||||||
r#"
|
|
||||||
echo "24 / 2" | math eval
|
|
||||||
"#
|
|
||||||
));
|
|
||||||
|
|
||||||
assert!(actual.out.contains("12"));
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn evaluates_twenty_eight_minus_seven() {
|
|
||||||
let actual = nu!(
|
|
||||||
cwd: ".", pipeline(
|
|
||||||
r#"
|
|
||||||
echo "28 - 7" | math eval
|
|
||||||
"#
|
|
||||||
));
|
|
||||||
|
|
||||||
assert!(actual.out.contains("21"));
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn evaluates_pi() {
|
|
||||||
let actual = nu!(
|
|
||||||
cwd: ".", pipeline(
|
|
||||||
r#"
|
|
||||||
math eval pi
|
|
||||||
"#
|
|
||||||
));
|
|
||||||
|
|
||||||
assert!(actual.out.contains("3.14"));
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn evaluates_tau() {
|
|
||||||
let actual = nu!(
|
|
||||||
cwd: ".", pipeline(
|
|
||||||
r#"
|
|
||||||
math eval tau
|
|
||||||
"#
|
|
||||||
));
|
|
||||||
|
|
||||||
assert!(actual.out.contains("6.28"));
|
|
||||||
}
|
|
@ -1,5 +1,4 @@
|
|||||||
mod avg;
|
mod avg;
|
||||||
mod eval;
|
|
||||||
mod median;
|
mod median;
|
||||||
mod round;
|
mod round;
|
||||||
mod sqrt;
|
mod sqrt;
|
||||||
|
@ -8,7 +8,7 @@ fn reduce_table_column() {
|
|||||||
echo "[{month:2,total:30}, {month:3,total:10}, {month:4,total:3}, {month:5,total:60}]"
|
echo "[{month:2,total:30}, {month:3,total:10}, {month:4,total:3}, {month:5,total:60}]"
|
||||||
| from json
|
| from json
|
||||||
| get total
|
| get total
|
||||||
| reduce -f 20 { |it, acc| $it + (math eval $"($acc)^1.05")}
|
| reduce -f 20 { |it, acc| $it + $acc ** 1.05}
|
||||||
| into string -d 1
|
| into string -d 1
|
||||||
"#
|
"#
|
||||||
)
|
)
|
||||||
@ -23,7 +23,7 @@ fn reduce_table_column_with_path() {
|
|||||||
cwd: ".", pipeline(
|
cwd: ".", pipeline(
|
||||||
r#"
|
r#"
|
||||||
[{month:2,total:30}, {month:3,total:10}, {month:4,total:3}, {month:5,total:60}]
|
[{month:2,total:30}, {month:3,total:10}, {month:4,total:3}, {month:5,total:60}]
|
||||||
| reduce -f 20 { |it, acc| $it.total + (math eval $"($acc)^1.05")}
|
| reduce -f 20 { |it, acc| $it.total + $acc ** 1.05}
|
||||||
| into string -d 1
|
| into string -d 1
|
||||||
"#
|
"#
|
||||||
)
|
)
|
||||||
|
@ -38,16 +38,3 @@ fn construct_a_path() {
|
|||||||
|
|
||||||
assert_eq!(actual.out, "sample.txt");
|
assert_eq!(actual.out, "sample.txt");
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn sum_one_to_four() {
|
|
||||||
let actual = nu!(
|
|
||||||
cwd: ".", pipeline(
|
|
||||||
r#"
|
|
||||||
1..4 | each { |it| $it } | into string | str join "+" | math eval
|
|
||||||
"#
|
|
||||||
)
|
|
||||||
);
|
|
||||||
|
|
||||||
assert!(actual.out.contains("10"));
|
|
||||||
}
|
|
||||||
|
Loading…
Reference in New Issue
Block a user