mirror of
https://github.com/nushell/nushell
synced 2024-12-28 22:13:10 +00:00
final math abs
This commit is contained in:
parent
51bea2e884
commit
aa5ab8a666
5 changed files with 86 additions and 22 deletions
|
@ -51,6 +51,8 @@ pub fn create_default_context() -> Rc<RefCell<EngineState>> {
|
||||||
LetEnv,
|
LetEnv,
|
||||||
Lines,
|
Lines,
|
||||||
Ls,
|
Ls,
|
||||||
|
Math,
|
||||||
|
MathAbs,
|
||||||
Mkdir,
|
Mkdir,
|
||||||
Module,
|
Module,
|
||||||
Mv,
|
Mv,
|
||||||
|
|
|
@ -7,8 +7,8 @@ mod experimental;
|
||||||
mod filesystem;
|
mod filesystem;
|
||||||
mod filters;
|
mod filters;
|
||||||
mod formats;
|
mod formats;
|
||||||
mod strings;
|
|
||||||
mod math;
|
mod math;
|
||||||
|
mod strings;
|
||||||
mod system;
|
mod system;
|
||||||
mod viewers;
|
mod viewers;
|
||||||
|
|
||||||
|
@ -21,7 +21,7 @@ pub use experimental::*;
|
||||||
pub use filesystem::*;
|
pub use filesystem::*;
|
||||||
pub use filters::*;
|
pub use filters::*;
|
||||||
pub use formats::*;
|
pub use formats::*;
|
||||||
pub use strings::*;
|
|
||||||
pub use math::*;
|
pub use math::*;
|
||||||
|
pub use strings::*;
|
||||||
pub use system::*;
|
pub use system::*;
|
||||||
pub use viewers::*;
|
pub use viewers::*;
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
use nu_protocol::ast::Call;
|
use nu_protocol::ast::Call;
|
||||||
use nu_protocol::engine::{Command, EvaluationContext};
|
use nu_protocol::engine::{Command, EvaluationContext};
|
||||||
use nu_protocol::{Example, ShellError, Signature, Span, Type, Value};
|
use nu_protocol::{Example, ShellError, Signature, Span, Value};
|
||||||
|
|
||||||
pub struct SubCommand;
|
pub struct SubCommand;
|
||||||
|
|
||||||
|
@ -17,23 +17,39 @@ impl Command for SubCommand {
|
||||||
"Returns absolute values of a list of numbers"
|
"Returns absolute values of a list of numbers"
|
||||||
}
|
}
|
||||||
|
|
||||||
fn run(&self, _context: &EvaluationContext, call: &Call, input: Value) -> Result<Value, ShellError> {
|
fn run(
|
||||||
|
&self,
|
||||||
|
_context: &EvaluationContext,
|
||||||
|
call: &Call,
|
||||||
|
input: Value,
|
||||||
|
) -> Result<Value, ShellError> {
|
||||||
let head = call.head;
|
let head = call.head;
|
||||||
input.map(head, move |val| match val {
|
match input {
|
||||||
Value::Int { val, span } => Value::int(val.abs(), span),
|
Value::List { vals, span } => Ok(Value::List {
|
||||||
Value::Float { val, span } => Value::Float{val: val.abs(), span: span},
|
vals: vals
|
||||||
Value::Duration { val, span } => Value::Duration{val: val.abs(), span: span},
|
.into_iter()
|
||||||
other => abs_default(other, head),
|
.map(move |val| abs_helper(val, head))
|
||||||
})
|
.collect(),
|
||||||
|
span,
|
||||||
|
}),
|
||||||
|
other => match abs_helper(other, head) {
|
||||||
|
Value::Error { error } => Err(error),
|
||||||
|
ok => Ok(ok),
|
||||||
|
},
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn examples(&self) -> Vec<Example> {
|
fn examples(&self) -> Vec<Example> {
|
||||||
vec![Example {
|
vec![Example {
|
||||||
description: "Get absolute of each value in a list of numbers",
|
description: "Get absolute of each value in a list of numbers",
|
||||||
example: "echo [-50 25] | math abs",
|
example: "echo [-50 -100.0 25] | math abs",
|
||||||
result: Some(Value:: List {
|
result: Some(Value::List {
|
||||||
vals: vec![
|
vals: vec![
|
||||||
Value::test_int(50),
|
Value::test_int(50),
|
||||||
|
Value::Float {
|
||||||
|
val: 100.0,
|
||||||
|
span: Span::unknown(),
|
||||||
|
},
|
||||||
Value::test_int(25),
|
Value::test_int(25),
|
||||||
],
|
],
|
||||||
span: Span::unknown(),
|
span: Span::unknown(),
|
||||||
|
@ -42,21 +58,33 @@ impl Command for SubCommand {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn abs_default(_: Value, head: Span) -> Value {
|
fn abs_helper(val: Value, head: Span) -> Value {
|
||||||
Value::Error {error: ShellError::UnsupportedInput(
|
match val {
|
||||||
String::from("Only numerical values are supported"),
|
Value::Int { val, span } => Value::int(val.abs(), span),
|
||||||
head
|
Value::Float { val, span } => Value::Float {
|
||||||
)}
|
val: val.abs(),
|
||||||
|
span,
|
||||||
|
},
|
||||||
|
Value::Duration { val, span } => Value::Duration {
|
||||||
|
val: val.abs(),
|
||||||
|
span,
|
||||||
|
},
|
||||||
|
_ => Value::Error {
|
||||||
|
error: ShellError::UnsupportedInput(
|
||||||
|
String::from("Only numerical values are supported"),
|
||||||
|
head,
|
||||||
|
),
|
||||||
|
},
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod test {
|
||||||
use super::ShellError;
|
use super::*;
|
||||||
use super::SubCommand;
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn examples_work_as_expected() -> Result<(), ShellError> {
|
fn test_examples() {
|
||||||
use crate::examples::test as test_examples;
|
use crate::test_examples;
|
||||||
|
|
||||||
test_examples(SubCommand {})
|
test_examples(SubCommand {})
|
||||||
}
|
}
|
||||||
|
|
32
crates/nu-command/src/math/command.rs
Normal file
32
crates/nu-command/src/math/command.rs
Normal file
|
@ -0,0 +1,32 @@
|
||||||
|
use nu_engine::get_full_help;
|
||||||
|
use nu_protocol::ast::Call;
|
||||||
|
use nu_protocol::engine::{Command, EvaluationContext};
|
||||||
|
use nu_protocol::{ShellError, Signature, Value};
|
||||||
|
|
||||||
|
pub struct MathCommand;
|
||||||
|
|
||||||
|
impl Command for MathCommand {
|
||||||
|
fn name(&self) -> &str {
|
||||||
|
"math"
|
||||||
|
}
|
||||||
|
|
||||||
|
fn signature(&self) -> Signature {
|
||||||
|
Signature::build("math")
|
||||||
|
}
|
||||||
|
|
||||||
|
fn usage(&self) -> &str {
|
||||||
|
"Use mathematical functions as aggregate functions on a list of numbers or tables."
|
||||||
|
}
|
||||||
|
|
||||||
|
fn run(
|
||||||
|
&self,
|
||||||
|
context: &EvaluationContext,
|
||||||
|
call: &Call,
|
||||||
|
_input: Value,
|
||||||
|
) -> Result<Value, ShellError> {
|
||||||
|
Ok(Value::String {
|
||||||
|
val: get_full_help(&MathCommand.signature(), &MathCommand.examples(), context),
|
||||||
|
span: call.head,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,3 +1,5 @@
|
||||||
mod abs;
|
mod abs;
|
||||||
|
pub mod command;
|
||||||
|
|
||||||
pub use abs::SubCommand as MathAbs;
|
pub use abs::SubCommand as MathAbs;
|
||||||
|
pub use command::MathCommand as Math;
|
||||||
|
|
Loading…
Reference in a new issue