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
|
||||
pub fn average(arg: &Value) -> EvalResult {
|
||||
let arguments = arg.as_tuple()?;
|
||||
let count = arguments.len() as i64;
|
||||
let mut is_float = false;
|
||||
let mut total_i = 0i64;
|
||||
let mut total_f = 0f64;
|
||||
if let Value::Tuple(args) = arg {
|
||||
let len = args.len() as f64;
|
||||
let mut total = 0f64;
|
||||
|
||||
for argument in arguments {
|
||||
if let Value::Float(float) = argument {
|
||||
if !is_float {
|
||||
total_f = total_i as f64;
|
||||
is_float = true;
|
||||
}
|
||||
total_f += float;
|
||||
} else if let Value::Int(int) = argument {
|
||||
if is_float {
|
||||
total_f += int as f64;
|
||||
} else {
|
||||
total_i += int;
|
||||
}
|
||||
for arg in args {
|
||||
total +=
|
||||
if let Value::Float(float) = arg { float.clone() }
|
||||
else if let Value::Int(int) = arg { int.clone() as f64 }
|
||||
else { todo!() };
|
||||
}
|
||||
}
|
||||
|
||||
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 {
|
||||
result_f = total_f / (count as f64);
|
||||
return Ok(result_f.into());
|
||||
} else {
|
||||
result_i = total_i / count;
|
||||
return Ok(result_i.into());
|
||||
}
|
||||
Ok( (total / len).into() )
|
||||
} else { todo!() }
|
||||
}
|
||||
|
||||
// Radix conversion
|
||||
pub fn binary(arg: &Value) -> EvalResult {
|
||||
if !arg.is_string() {
|
||||
let num = arg.as_int()?;
|
||||
let fmt = format!("0b{:b}", num);
|
||||
return Ok(fmt.into());
|
||||
}
|
||||
util::parse_radix("0b", 2, arg)
|
||||
if let Value::Int(int) = arg { Ok( format!("0b{:b}", int).into() ) }
|
||||
else if let Value::String(string) = arg { util::parse_radix("0b", 2, string) }
|
||||
else { Err(EvalexprError::expected_number_or_string(arg.clone())) }
|
||||
}
|
||||
|
||||
pub fn hexadecimal(arg: &Value) -> EvalResult {
|
||||
if !arg.is_string() {
|
||||
let num = arg.as_int()?;
|
||||
let fmt = format!("0x{:X}", num);
|
||||
return Ok(fmt.into());
|
||||
}
|
||||
util::parse_radix("0x", 16, arg)
|
||||
if let Value::Int(int) = arg { Ok( format!("0x{:X}", int).into() ) }
|
||||
else if let Value::String(string) = arg { util::parse_radix("0x", 16, string) }
|
||||
else { Err(EvalexprError::expected_number_or_string(arg.clone())) }
|
||||
}
|
||||
|
||||
pub fn octal(arg: &Value) -> EvalResult {
|
||||
if !arg.is_string() {
|
||||
let num = arg.as_int()?;
|
||||
let fmt = format!("{:#o}", num);
|
||||
return Ok(fmt.into());
|
||||
}
|
||||
util::parse_radix("0o", 8, arg)
|
||||
if let Value::Int(int) = arg { Ok( format!("0o{:#o}", int).into() ) }
|
||||
else if let Value::String(string) = arg { util::parse_radix("0o", 8, string) }
|
||||
else { Err(EvalexprError::expected_number_or_string(arg.clone())) }
|
||||
}
|
||||
|
||||
|
|
14
src/util.rs
14
src/util.rs
|
@ -1,16 +1,10 @@
|
|||
|
||||
use evalexpr::{ EvalexprError, Value };
|
||||
|
||||
pub(crate) fn parse_radix(prefix: &str, base: u32, arg: &Value) -> Result<Value, EvalexprError> {
|
||||
let i_parse = arg.as_string()?;
|
||||
let parse = i_parse.strip_prefix(prefix).unwrap_or(i_parse.as_str());
|
||||
pub fn parse_radix(prefix: &str, base: u32, arg: &str) -> Result<Value, EvalexprError> {
|
||||
let parse = arg.strip_prefix(prefix).unwrap_or(arg);
|
||||
|
||||
let i_result = i64::from_str_radix(parse, base);
|
||||
if i_result.is_err() {
|
||||
return Err(EvalexprError::CustomMessage("failed to parse integer from string".to_string()));
|
||||
}
|
||||
let result = i_result.ok();
|
||||
|
||||
return Ok(result.unwrap().into());
|
||||
if let Ok(int) = i64::from_str_radix(parse, base) { Ok(int.into()) }
|
||||
else { Err(EvalexprError::CustomMessage("failed to parse integer from string".to_string())) }
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue