mirror of
https://github.com/nushell/nushell
synced 2024-11-11 07:34:14 +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,
|
||||
Lines,
|
||||
Ls,
|
||||
Math,
|
||||
MathAbs,
|
||||
Mkdir,
|
||||
Module,
|
||||
Mv,
|
||||
|
|
|
@ -7,8 +7,8 @@ mod experimental;
|
|||
mod filesystem;
|
||||
mod filters;
|
||||
mod formats;
|
||||
mod strings;
|
||||
mod math;
|
||||
mod strings;
|
||||
mod system;
|
||||
mod viewers;
|
||||
|
||||
|
@ -21,7 +21,7 @@ pub use experimental::*;
|
|||
pub use filesystem::*;
|
||||
pub use filters::*;
|
||||
pub use formats::*;
|
||||
pub use strings::*;
|
||||
pub use math::*;
|
||||
pub use strings::*;
|
||||
pub use system::*;
|
||||
pub use viewers::*;
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
use nu_protocol::ast::Call;
|
||||
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;
|
||||
|
||||
|
@ -17,23 +17,39 @@ impl Command for SubCommand {
|
|||
"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;
|
||||
input.map(head, move |val| match val {
|
||||
Value::Int { val, span } => Value::int(val.abs(), span),
|
||||
Value::Float { val, span } => Value::Float{val: val.abs(), span: span},
|
||||
Value::Duration { val, span } => Value::Duration{val: val.abs(), span: span},
|
||||
other => abs_default(other, head),
|
||||
})
|
||||
match input {
|
||||
Value::List { vals, span } => Ok(Value::List {
|
||||
vals: vals
|
||||
.into_iter()
|
||||
.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> {
|
||||
vec![Example {
|
||||
description: "Get absolute of each value in a list of numbers",
|
||||
example: "echo [-50 25] | math abs",
|
||||
result: Some(Value:: List {
|
||||
example: "echo [-50 -100.0 25] | math abs",
|
||||
result: Some(Value::List {
|
||||
vals: vec![
|
||||
Value::test_int(50),
|
||||
Value::Float {
|
||||
val: 100.0,
|
||||
span: Span::unknown(),
|
||||
},
|
||||
Value::test_int(25),
|
||||
],
|
||||
span: Span::unknown(),
|
||||
|
@ -42,21 +58,33 @@ impl Command for SubCommand {
|
|||
}
|
||||
}
|
||||
|
||||
fn abs_default(_: Value, head: Span) -> Value {
|
||||
Value::Error {error: ShellError::UnsupportedInput(
|
||||
String::from("Only numerical values are supported"),
|
||||
head
|
||||
)}
|
||||
fn abs_helper(val: Value, head: Span) -> Value {
|
||||
match val {
|
||||
Value::Int { val, span } => Value::int(val.abs(), span),
|
||||
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)]
|
||||
mod tests {
|
||||
use super::ShellError;
|
||||
use super::SubCommand;
|
||||
mod test {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn examples_work_as_expected() -> Result<(), ShellError> {
|
||||
use crate::examples::test as test_examples;
|
||||
fn test_examples() {
|
||||
use crate::test_examples;
|
||||
|
||||
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;
|
||||
pub mod command;
|
||||
|
||||
pub use abs::SubCommand as MathAbs;
|
||||
pub use command::MathCommand as Math;
|
||||
|
|
Loading…
Reference in a new issue