Skip to content

Commit

Permalink
implement Copy on Number
Browse files Browse the repository at this point in the history
  • Loading branch information
adamtrilling committed Apr 22, 2022
1 parent 51d493a commit 6f16986
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 12 deletions.
4 changes: 2 additions & 2 deletions rust/src/function/sum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ pub fn sum(mut arg_itr: Pairs<Rule>, data: &Value, context_opt: Option<Value>) -

match arg {
Value::Array(arr) => arr
.iter()
.try_fold(Value::Number(Number::Int(0)), |acc, x| add(acc, x.clone())),
.into_iter()
.try_fold(Value::Number(Number::Int(0)), |acc, x| add(acc, x)),
_ => Err(Error::eval(format!(
"argument to sum must be an array (got {:?}",
arg
Expand Down
18 changes: 9 additions & 9 deletions rust/src/function/summarize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub fn summarize(
let mut result = BTreeMap::new();
result.insert("max".to_string(), max(&nums));
result.insert("min".to_string(), min(&nums));
result.insert("mean".to_string(), Value::Number(mean.clone()));
result.insert("mean".to_string(), Value::Number(mean));
result.insert("median".to_string(), median(&nums));
result.insert("variance".to_string(), variance.clone());
result.insert("stddev".to_string(), sqrt(variance)?);
Expand All @@ -47,27 +47,27 @@ pub fn summarize(

fn max(arr: &Vec<Number>) -> Value {
// safe unwrap - we know the vec has at least one element
Value::Number(arr.last().unwrap().clone())
Value::Number(*arr.last().unwrap())
}

fn min(arr: &Vec<Number>) -> Value {
// safe unwrap - we know the vec has at least one element
Value::Number(arr.first().unwrap().clone())
Value::Number(*arr.first().unwrap())
}

fn mean(arr: &Vec<Number>) -> Number {
let sum = arr.iter().fold(Number::Int(0), |acc, x| acc + x.clone());
let sum = arr.iter().fold(Number::Int(0), |acc, x| acc + *x);
sum / Number::Int(arr.len() as i64)
}

fn median(arr: &Vec<Number>) -> Value {
let res = match arr.len() {
even if even % 2 == 0 => {
let low = arr[even / 2].clone();
let high = arr[(even / 2) + 1].clone();
let low = arr[even / 2];
let high = arr[(even / 2) + 1];
(low + high) / Number::Int(2)
}
odd => arr[odd / 2].clone(),
odd => arr[odd / 2],
};
Value::Number(res)
}
Expand All @@ -76,8 +76,8 @@ fn variance(arr: &Vec<Number>, mean: &Number) -> Value {
let sum_of_diffs = arr
.iter()
.map(|val| {
let diff = mean.clone() - val.clone();
diff.clone() * diff
let diff = *mean - *val;
diff * diff
})
.fold(Number::Float(0.0), |acc, x| acc + x);
Value::Number(sum_of_diffs / Number::Int(arr.len() as i64 - 1))
Expand Down
2 changes: 1 addition & 1 deletion rust/src/value/number.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::{Error, Result};
use std::cmp::Ordering;
use std::ops::{Add, Div, Mul, Sub};

#[derive(Clone, Debug)]
#[derive(Clone, Copy, Debug)]
pub enum Number {
Int(i64),
Float(f64),
Expand Down

0 comments on commit 6f16986

Please sign in to comment.