refactored other helper methods
This commit is contained in:
parent
6a74756835
commit
8c1610bcf9
2 changed files with 24 additions and 61 deletions
|
@ -104,68 +104,37 @@ pub fn tangent(arg: &Value) -> EvalResult {
|
||||||
|
|
||||||
// Data Science
|
// Data Science
|
||||||
pub fn average(arg: &Value) -> EvalResult {
|
pub fn average(arg: &Value) -> EvalResult {
|
||||||
let arguments = arg.as_tuple()?;
|
if let Value::Tuple(args) = arg {
|
||||||
let count = arguments.len() as i64;
|
let len = args.len() as f64;
|
||||||
let mut is_float = false;
|
let mut total = 0f64;
|
||||||
let mut total_i = 0i64;
|
|
||||||
let mut total_f = 0f64;
|
|
||||||
|
|
||||||
for argument in arguments {
|
for arg in args {
|
||||||
if let Value::Float(float) = argument {
|
total +=
|
||||||
if !is_float {
|
if let Value::Float(float) = arg { float.clone() }
|
||||||
total_f = total_i as f64;
|
else if let Value::Int(int) = arg { int.clone() as f64 }
|
||||||
is_float = true;
|
else { todo!() };
|
||||||
}
|
|
||||||
total_f += float;
|
|
||||||
} else if let Value::Int(int) = argument {
|
|
||||||
if is_float {
|
|
||||||
total_f += int as f64;
|
|
||||||
} else {
|
|
||||||
total_i += int;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
let result_i: i64;
|
|
||||||
let result_f: f64;
|
|
||||||
if !is_float {
|
|
||||||
is_float = total_i % count == 0;
|
|
||||||
total_f = total_i as f64;
|
|
||||||
}
|
|
||||||
|
|
||||||
if is_float {
|
Ok( (total / len).into() )
|
||||||
result_f = total_f / (count as f64);
|
} else { todo!() }
|
||||||
return Ok(result_f.into());
|
|
||||||
} else {
|
|
||||||
result_i = total_i / count;
|
|
||||||
return Ok(result_i.into());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Radix conversion
|
// Radix conversion
|
||||||
pub fn binary(arg: &Value) -> EvalResult {
|
pub fn binary(arg: &Value) -> EvalResult {
|
||||||
if !arg.is_string() {
|
if let Value::Int(int) = arg { Ok( format!("0b{:b}", int).into() ) }
|
||||||
let num = arg.as_int()?;
|
else if let Value::String(string) = arg { util::parse_radix("0b", 2, string) }
|
||||||
let fmt = format!("0b{:b}", num);
|
else { Err(EvalexprError::expected_number_or_string(arg.clone())) }
|
||||||
return Ok(fmt.into());
|
|
||||||
}
|
|
||||||
util::parse_radix("0b", 2, arg)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn hexadecimal(arg: &Value) -> EvalResult {
|
pub fn hexadecimal(arg: &Value) -> EvalResult {
|
||||||
if !arg.is_string() {
|
if let Value::Int(int) = arg { Ok( format!("0x{:X}", int).into() ) }
|
||||||
let num = arg.as_int()?;
|
else if let Value::String(string) = arg { util::parse_radix("0x", 16, string) }
|
||||||
let fmt = format!("0x{:X}", num);
|
else { Err(EvalexprError::expected_number_or_string(arg.clone())) }
|
||||||
return Ok(fmt.into());
|
|
||||||
}
|
|
||||||
util::parse_radix("0x", 16, arg)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn octal(arg: &Value) -> EvalResult {
|
pub fn octal(arg: &Value) -> EvalResult {
|
||||||
if !arg.is_string() {
|
if let Value::Int(int) = arg { Ok( format!("0o{:#o}", int).into() ) }
|
||||||
let num = arg.as_int()?;
|
else if let Value::String(string) = arg { util::parse_radix("0o", 8, string) }
|
||||||
let fmt = format!("{:#o}", num);
|
else { Err(EvalexprError::expected_number_or_string(arg.clone())) }
|
||||||
return Ok(fmt.into());
|
|
||||||
}
|
|
||||||
util::parse_radix("0o", 8, arg)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
14
src/util.rs
14
src/util.rs
|
@ -1,16 +1,10 @@
|
||||||
|
|
||||||
use evalexpr::{ EvalexprError, Value };
|
use evalexpr::{ EvalexprError, Value };
|
||||||
|
|
||||||
pub(crate) fn parse_radix(prefix: &str, base: u32, arg: &Value) -> Result<Value, EvalexprError> {
|
pub fn parse_radix(prefix: &str, base: u32, arg: &str) -> Result<Value, EvalexprError> {
|
||||||
let i_parse = arg.as_string()?;
|
let parse = arg.strip_prefix(prefix).unwrap_or(arg);
|
||||||
let parse = i_parse.strip_prefix(prefix).unwrap_or(i_parse.as_str());
|
|
||||||
|
|
||||||
let i_result = i64::from_str_radix(parse, base);
|
if let Ok(int) = i64::from_str_radix(parse, base) { Ok(int.into()) }
|
||||||
if i_result.is_err() {
|
else { Err(EvalexprError::CustomMessage("failed to parse integer from string".to_string())) }
|
||||||
return Err(EvalexprError::CustomMessage("failed to parse integer from string".to_string()));
|
|
||||||
}
|
|
||||||
let result = i_result.ok();
|
|
||||||
|
|
||||||
return Ok(result.unwrap().into());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue