From 03c2ace0af4a45894dd5dace91834801ee39f37c Mon Sep 17 00:00:00 2001 From: m5l14i11 Date: Fri, 14 Jul 2023 10:50:23 +0300 Subject: [PATCH] refactor --- ta_lib/core/src/ops.rs | 30 +++++++++++++++--------------- ta_lib/core/src/series.rs | 10 +++++----- 2 files changed, 20 insertions(+), 20 deletions(-) diff --git a/ta_lib/core/src/ops.rs b/ta_lib/core/src/ops.rs index 19e32f9a..f69807f0 100644 --- a/ta_lib/core/src/ops.rs +++ b/ta_lib/core/src/ops.rs @@ -3,32 +3,32 @@ use std::ops::{Add, Div, Mul, Neg, Sub}; impl Series { pub fn add_series(&self, rhs: &Series) -> Series { - self.clone().zip_with(rhs, |a, b| match (a, b) { - (Some(a_val), Some(b_val)) => Some(a_val + b_val), + self.zip_with(rhs, |a, b| match (a, b) { + (Some(a_val), Some(b_val)) => Some(*a_val + *b_val), _ => None, }) } pub fn mul_series(&self, rhs: &Series) -> Series { - self.clone().zip_with(rhs, |a, b| match (a, b) { - (Some(a_val), Some(b_val)) => Some(a_val * b_val), + self.zip_with(rhs, |a, b| match (a, b) { + (Some(a_val), Some(b_val)) => Some(*a_val * *b_val), _ => None, }) } pub fn div_series(&self, rhs: &Series) -> Series { - self.clone().zip_with(rhs, |a, b| match (a, b) { + self.zip_with(rhs, |a, b| match (a, b) { (Some(a_val), Some(b_val)) => { - if b_val == 0.0 { - if a_val > 0.0 { + if *b_val == 0.0 { + if *a_val > 0.0 { Some(std::f64::INFINITY) - } else if a_val < 0.0 { + } else if *a_val < 0.0 { Some(std::f64::NEG_INFINITY) } else { None } } else { - Some(a_val / b_val) + Some(*a_val / *b_val) } } _ => None, @@ -36,8 +36,8 @@ impl Series { } pub fn sub_series(&self, rhs: &Series) -> Series { - self.clone().zip_with(rhs, |a, b| match (a, b) { - (Some(a_val), Some(b_val)) => Some(a_val - b_val), + self.zip_with(rhs, |a, b| match (a, b) { + (Some(a_val), Some(b_val)) => Some(*a_val - *b_val), _ => None, }) } @@ -79,10 +79,10 @@ impl Series { impl Series { pub fn mul_series(&self, rhs: &Series) -> Series { - self.clone().zip_with(rhs, |b, val| match (b, val) { - (Some(b_val), Some(val_val)) => { - if b_val { - Some(val_val) + self.zip_with(rhs, |b, a| match (b, a) { + (Some(b_val), Some(a_val)) => { + if *b_val { + Some(*a_val) } else { Some(0.0) } diff --git a/ta_lib/core/src/series.rs b/ta_lib/core/src/series.rs index bd7c9444..284e1880 100644 --- a/ta_lib/core/src/series.rs +++ b/ta_lib/core/src/series.rs @@ -16,16 +16,16 @@ impl Series { } } - pub fn zip_with(self, other: &Series, mut f: F) -> Series + pub fn zip_with(&self, other: &Series, mut f: F) -> Series where - F: FnMut(Option, Option) -> Option, + F: FnMut(Option<&T>, Option<&U>) -> Option, U: Clone, { let data = self .data - .into_iter() - .zip(other.data.clone().into_iter()) - .map(|(x, y)| f(x, y)) + .iter() + .zip(&other.data) + .map(|(x, y)| f(x.as_ref(), y.as_ref())) .collect(); Series { data }