Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
m5l14i11 committed Jul 14, 2023
1 parent 567c71c commit 03c2ace
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 20 deletions.
30 changes: 15 additions & 15 deletions ta_lib/core/src/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,41 +3,41 @@ use std::ops::{Add, Div, Mul, Neg, Sub};

impl Series<f64> {
pub fn add_series(&self, rhs: &Series<f64>) -> Series<f64> {
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<f64>) -> Series<f64> {
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<f64>) -> Series<f64> {
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,
})
}

pub fn sub_series(&self, rhs: &Series<f64>) -> Series<f64> {
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,
})
}
Expand Down Expand Up @@ -79,10 +79,10 @@ impl Series<f64> {

impl Series<bool> {
pub fn mul_series(&self, rhs: &Series<f64>) -> Series<f64> {
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)
}
Expand Down
10 changes: 5 additions & 5 deletions ta_lib/core/src/series.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@ impl<T: Clone> Series<T> {
}
}

pub fn zip_with<U, V, F>(self, other: &Series<U>, mut f: F) -> Series<V>
pub fn zip_with<U, V, F>(&self, other: &Series<U>, mut f: F) -> Series<V>
where
F: FnMut(Option<T>, Option<U>) -> Option<V>,
F: FnMut(Option<&T>, Option<&U>) -> Option<V>,
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 }
Expand Down

0 comments on commit 03c2ace

Please sign in to comment.