use crate::chart::Chart;
use crate::core::data::{ColumnVector, RowAccessor};
use crate::core::utils::IntoParallelizable;
use crate::error::ChartonError;
use crate::mark::Mark;
#[cfg(feature = "parallel")]
use rayon::prelude::*;
impl<T: Mark> Chart<T> {
pub fn transform_calculate<F>(mut self, as_name: &str, f: F) -> Result<Self, ChartonError>
where
F: Fn(RowAccessor) -> Option<f64> + Sync + Send,
{
let row_count = self.data.height();
if row_count == 0 {
return Ok(self);
}
let ds_ref = &self.data;
let new_data: Vec<f64> = (0..row_count)
.maybe_into_par_iter()
.map(|i| f(RowAccessor::new(ds_ref, i)).unwrap_or(f64::NAN))
.collect();
self.data
.add_column(as_name, ColumnVector::F64 { data: new_data })?;
Ok(self)
}
}