2022-05-14 12:35:08 +00:00
|
|
|
use crate::ctx::Context;
|
2022-01-13 17:37:46 +00:00
|
|
|
use crate::err::Error;
|
|
|
|
use crate::fnc::util::math::bottom::Bottom;
|
|
|
|
use crate::fnc::util::math::deviation::Deviation;
|
|
|
|
use crate::fnc::util::math::interquartile::Interquartile;
|
|
|
|
use crate::fnc::util::math::mean::Mean;
|
|
|
|
use crate::fnc::util::math::median::Median;
|
|
|
|
use crate::fnc::util::math::midhinge::Midhinge;
|
|
|
|
use crate::fnc::util::math::mode::Mode;
|
|
|
|
use crate::fnc::util::math::nearestrank::Nearestrank;
|
|
|
|
use crate::fnc::util::math::percentile::Percentile;
|
|
|
|
use crate::fnc::util::math::spread::Spread;
|
|
|
|
use crate::fnc::util::math::top::Top;
|
|
|
|
use crate::fnc::util::math::trimean::Trimean;
|
|
|
|
use crate::fnc::util::math::variance::Variance;
|
|
|
|
use crate::sql::number::Number;
|
|
|
|
use crate::sql::value::Value;
|
|
|
|
|
2022-05-14 12:35:08 +00:00
|
|
|
pub fn abs(_: &Context, mut args: Vec<Value>) -> Result<Value, Error> {
|
2022-01-13 17:37:46 +00:00
|
|
|
Ok(args.remove(0).as_number().abs().into())
|
|
|
|
}
|
|
|
|
|
2022-05-14 12:35:08 +00:00
|
|
|
pub fn bottom(_: &Context, mut args: Vec<Value>) -> Result<Value, Error> {
|
2022-01-13 17:37:46 +00:00
|
|
|
match args.remove(0) {
|
2022-03-04 16:01:32 +00:00
|
|
|
Value::Array(v) => {
|
|
|
|
let c = args.remove(0).as_int();
|
|
|
|
Ok(v.as_numbers().bottom(c).into())
|
|
|
|
}
|
2022-01-13 17:37:46 +00:00
|
|
|
_ => Ok(Value::None),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-14 12:35:08 +00:00
|
|
|
pub fn ceil(_: &Context, mut args: Vec<Value>) -> Result<Value, Error> {
|
2022-01-13 17:37:46 +00:00
|
|
|
Ok(args.remove(0).as_number().ceil().into())
|
|
|
|
}
|
|
|
|
|
2022-05-14 12:35:08 +00:00
|
|
|
pub fn fixed(_: &Context, mut args: Vec<Value>) -> Result<Value, Error> {
|
2022-03-04 16:01:32 +00:00
|
|
|
let v = args.remove(0);
|
|
|
|
match args.remove(0).as_int() {
|
|
|
|
p if p > 0 => Ok(v.as_number().fixed(p as usize).into()),
|
2022-03-06 10:58:59 +00:00
|
|
|
_ => Err(Error::InvalidArguments {
|
2022-03-04 16:01:32 +00:00
|
|
|
name: String::from("math::fixed"),
|
|
|
|
message: String::from("The second argument must be an integer greater than 0."),
|
|
|
|
}),
|
2022-01-13 17:37:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-14 12:35:08 +00:00
|
|
|
pub fn floor(_: &Context, mut args: Vec<Value>) -> Result<Value, Error> {
|
2022-01-13 17:37:46 +00:00
|
|
|
Ok(args.remove(0).as_number().floor().into())
|
|
|
|
}
|
|
|
|
|
2022-05-14 12:35:08 +00:00
|
|
|
pub fn interquartile(_: &Context, mut args: Vec<Value>) -> Result<Value, Error> {
|
2022-01-13 17:37:46 +00:00
|
|
|
match args.remove(0) {
|
|
|
|
Value::Array(v) => Ok(v.as_numbers().interquartile().into()),
|
|
|
|
_ => Ok(Value::None),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-14 12:35:08 +00:00
|
|
|
pub fn max(_: &Context, mut args: Vec<Value>) -> Result<Value, Error> {
|
2022-01-13 17:37:46 +00:00
|
|
|
match args.remove(0) {
|
|
|
|
Value::Array(v) => match v.as_numbers().into_iter().max() {
|
|
|
|
Some(v) => Ok(v.into()),
|
|
|
|
None => Ok(Value::None),
|
|
|
|
},
|
|
|
|
v => Ok(v),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-14 12:35:08 +00:00
|
|
|
pub fn mean(_: &Context, mut args: Vec<Value>) -> Result<Value, Error> {
|
2022-01-13 17:37:46 +00:00
|
|
|
match args.remove(0) {
|
|
|
|
Value::Array(v) => Ok(v.as_numbers().mean().into()),
|
|
|
|
_ => Ok(Value::None),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-14 12:35:08 +00:00
|
|
|
pub fn median(_: &Context, mut args: Vec<Value>) -> Result<Value, Error> {
|
2022-01-13 17:37:46 +00:00
|
|
|
match args.remove(0) {
|
|
|
|
Value::Array(v) => Ok(v.as_numbers().median().into()),
|
|
|
|
_ => Ok(Value::None),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-14 12:35:08 +00:00
|
|
|
pub fn midhinge(_: &Context, mut args: Vec<Value>) -> Result<Value, Error> {
|
2022-01-13 17:37:46 +00:00
|
|
|
match args.remove(0) {
|
|
|
|
Value::Array(v) => Ok(v.as_numbers().midhinge().into()),
|
|
|
|
_ => Ok(Value::None),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-14 12:35:08 +00:00
|
|
|
pub fn min(_: &Context, mut args: Vec<Value>) -> Result<Value, Error> {
|
2022-01-13 17:37:46 +00:00
|
|
|
match args.remove(0) {
|
|
|
|
Value::Array(v) => match v.as_numbers().into_iter().min() {
|
|
|
|
Some(v) => Ok(v.into()),
|
|
|
|
None => Ok(Value::None),
|
|
|
|
},
|
|
|
|
v => Ok(v),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-14 12:35:08 +00:00
|
|
|
pub fn mode(_: &Context, mut args: Vec<Value>) -> Result<Value, Error> {
|
2022-01-13 17:37:46 +00:00
|
|
|
match args.remove(0) {
|
|
|
|
Value::Array(v) => Ok(v.as_numbers().mode().into()),
|
|
|
|
_ => Ok(Value::None),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-14 12:35:08 +00:00
|
|
|
pub fn nearestrank(_: &Context, mut args: Vec<Value>) -> Result<Value, Error> {
|
2022-01-13 17:37:46 +00:00
|
|
|
match args.remove(0) {
|
|
|
|
Value::Array(v) => Ok(v.as_numbers().nearestrank(args.remove(0).as_number()).into()),
|
|
|
|
_ => Ok(Value::None),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-14 12:35:08 +00:00
|
|
|
pub fn percentile(_: &Context, mut args: Vec<Value>) -> Result<Value, Error> {
|
2022-01-13 17:37:46 +00:00
|
|
|
match args.remove(0) {
|
|
|
|
Value::Array(v) => Ok(v.as_numbers().percentile(args.remove(0).as_number()).into()),
|
|
|
|
_ => Ok(Value::None),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-14 12:35:08 +00:00
|
|
|
pub fn product(_: &Context, mut args: Vec<Value>) -> Result<Value, Error> {
|
2022-01-13 17:37:46 +00:00
|
|
|
match args.remove(0) {
|
|
|
|
Value::Array(v) => Ok(v.as_numbers().into_iter().product::<Number>().into()),
|
|
|
|
_ => Ok(Value::None),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-14 12:35:08 +00:00
|
|
|
pub fn round(_: &Context, mut args: Vec<Value>) -> Result<Value, Error> {
|
2022-01-13 17:37:46 +00:00
|
|
|
Ok(args.remove(0).as_number().round().into())
|
|
|
|
}
|
|
|
|
|
2022-05-14 12:35:08 +00:00
|
|
|
pub fn spread(_: &Context, mut args: Vec<Value>) -> Result<Value, Error> {
|
2022-01-13 17:37:46 +00:00
|
|
|
match args.remove(0) {
|
|
|
|
Value::Array(v) => Ok(v.as_numbers().spread().into()),
|
|
|
|
_ => Ok(Value::None),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-14 12:35:08 +00:00
|
|
|
pub fn sqrt(_: &Context, mut args: Vec<Value>) -> Result<Value, Error> {
|
2022-01-13 17:37:46 +00:00
|
|
|
Ok(args.remove(0).as_number().sqrt().into())
|
|
|
|
}
|
|
|
|
|
2022-05-14 12:35:08 +00:00
|
|
|
pub fn stddev(_: &Context, mut args: Vec<Value>) -> Result<Value, Error> {
|
2022-01-13 17:37:46 +00:00
|
|
|
match args.remove(0) {
|
|
|
|
Value::Array(v) => Ok(v.as_numbers().deviation().into()),
|
|
|
|
_ => Ok(Value::None),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-14 12:35:08 +00:00
|
|
|
pub fn sum(_: &Context, mut args: Vec<Value>) -> Result<Value, Error> {
|
2022-01-13 17:37:46 +00:00
|
|
|
match args.remove(0) {
|
|
|
|
Value::Array(v) => Ok(v.as_numbers().into_iter().sum::<Number>().into()),
|
|
|
|
_ => Ok(Value::None),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-14 12:35:08 +00:00
|
|
|
pub fn top(_: &Context, mut args: Vec<Value>) -> Result<Value, Error> {
|
2022-01-13 17:37:46 +00:00
|
|
|
match args.remove(0) {
|
2022-03-04 16:01:32 +00:00
|
|
|
Value::Array(v) => {
|
|
|
|
let c = args.remove(0).as_int();
|
|
|
|
Ok(v.as_numbers().top(c).into())
|
|
|
|
}
|
2022-01-13 17:37:46 +00:00
|
|
|
_ => Ok(Value::None),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-14 12:35:08 +00:00
|
|
|
pub fn trimean(_: &Context, mut args: Vec<Value>) -> Result<Value, Error> {
|
2022-01-13 17:37:46 +00:00
|
|
|
match args.remove(0) {
|
|
|
|
Value::Array(v) => Ok(v.as_numbers().trimean().into()),
|
|
|
|
_ => Ok(Value::None),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-14 12:35:08 +00:00
|
|
|
pub fn variance(_: &Context, mut args: Vec<Value>) -> Result<Value, Error> {
|
2022-01-13 17:37:46 +00:00
|
|
|
match args.remove(0) {
|
|
|
|
Value::Array(v) => Ok(v.as_numbers().variance().into()),
|
|
|
|
_ => Ok(Value::None),
|
|
|
|
}
|
|
|
|
}
|