Skip to content

Commit

Permalink
remove try_apply_values
Browse files Browse the repository at this point in the history
  • Loading branch information
orlp committed Mar 28, 2024
1 parent bee7ed6 commit 69ea11f
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 105 deletions.
100 changes: 0 additions & 100 deletions crates/polars-core/src/chunked_array/ops/apply.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,27 +25,6 @@ where
ChunkedArray::from_chunk_iter(self.name(), iter)
}

/// Applies a function to all elements, regardless of whether they
/// are null or not, after which the null mask is copied from the
/// original array.
pub fn try_apply_values_generic<'a, U, K, F, E>(
&'a self,
mut op: F,
) -> Result<ChunkedArray<U>, E>
where
U: PolarsDataType,
F: FnMut(T::Physical<'a>) -> Result<K, E>,
U::Array: ArrayFromIter<K>,
{
let iter = self.downcast_iter().map(|arr| {
let element_iter = arr.values_iter().map(&mut op);
let array: U::Array = element_iter.try_collect_arr()?;
Ok(array.with_validity_typed(arr.validity().cloned()))
});

ChunkedArray::try_from_chunk_iter(self.name(), iter)
}

/// Applies a function only to the non-null elements, propagating nulls.
pub fn apply_nonnull_values_generic<'a, U, K, F>(
&'a self,
Expand Down Expand Up @@ -239,22 +218,6 @@ where
ChunkedArray::from_chunk_iter(self.name(), chunks)
}

fn try_apply_values<F>(&'a self, f: F) -> PolarsResult<Self>
where
F: Fn(T::Native) -> PolarsResult<T::Native> + Copy,
{
let mut ca: ChunkedArray<T> = self
.data_views()
.zip(self.iter_validities())
.map(|(slice, validity)| {
let vec: PolarsResult<Vec<_>> = slice.iter().copied().map(f).collect();
Ok((vec?, validity.cloned()))
})
.collect::<PolarsResult<_>>()?;
ca.rename(self.name());
Ok(ca)
}

fn apply<F>(&'a self, f: F) -> Self
where
F: Fn(Option<T::Native>) -> Option<T::Native> + Copy,
Expand Down Expand Up @@ -311,14 +274,6 @@ impl<'a> ChunkApply<'a, bool> for BooleanChunked {
}
}

fn try_apply_values<F>(&self, f: F) -> PolarsResult<Self>
where
F: Fn(bool) -> PolarsResult<bool> + Copy,
{
// Inefficient implementation but never actually used I believe.
self.try_apply_values_generic(f)
}

fn apply<F>(&'a self, f: F) -> Self
where
F: Fn(Option<bool>) -> Option<bool> + Copy,
Expand Down Expand Up @@ -398,13 +353,6 @@ impl<'a> ChunkApply<'a, &'a str> for StringChunked {
ChunkedArray::apply_values_generic(self, f)
}

fn try_apply_values<F>(&'a self, f: F) -> PolarsResult<Self>
where
F: Fn(&'a str) -> PolarsResult<Cow<'a, str>> + Copy,
{
self.try_apply_values_generic(f)
}

fn apply<F>(&'a self, f: F) -> Self
where
F: Fn(Option<&'a str>) -> Option<Cow<'a, str>> + Copy,
Expand Down Expand Up @@ -441,13 +389,6 @@ impl<'a> ChunkApply<'a, &'a [u8]> for BinaryChunked {
self.apply_values_generic(f)
}

fn try_apply_values<F>(&'a self, f: F) -> PolarsResult<Self>
where
F: Fn(&'a [u8]) -> PolarsResult<Cow<'a, [u8]>> + Copy,
{
self.try_apply_values_generic(f)
}

fn apply<F>(&'a self, f: F) -> Self
where
F: Fn(Option<&'a [u8]>) -> Option<Cow<'a, [u8]>> + Copy,
Expand Down Expand Up @@ -572,40 +513,6 @@ impl<'a> ChunkApply<'a, Series> for ListChunked {
ca
}

fn try_apply_values<F>(&'a self, f: F) -> PolarsResult<Self>
where
F: Fn(Series) -> PolarsResult<Series> + Copy,
{
if self.is_empty() {
return Ok(self.clone());
}

let mut fast_explode = true;
let mut function = |s: Series| {
let out = f(s);
if let Ok(out) = &out {
if out.is_empty() {
fast_explode = false;
}
}
out
};
let ca: PolarsResult<ListChunked> = {
if !self.has_validity() {
self.into_no_null_iter().map(&mut function).collect()
} else {
self.into_iter()
.map(|opt_v| opt_v.map(&mut function).transpose())
.collect()
}
};
let mut ca = ca?;
if fast_explode {
ca.set_fast_explode()
}
Ok(ca)
}

fn apply<F>(&'a self, f: F) -> Self
where
F: Fn(Option<Series>) -> Option<Series> + Copy,
Expand Down Expand Up @@ -653,13 +560,6 @@ where
ca
}

fn try_apply_values<F>(&'a self, _f: F) -> PolarsResult<Self>
where
F: Fn(&'a T) -> PolarsResult<T> + Copy,
{
todo!()
}

fn apply<F>(&'a self, f: F) -> Self
where
F: Fn(Option<&'a T>) -> Option<T> + Copy,
Expand Down
5 changes: 0 additions & 5 deletions crates/polars-core/src/chunked_array/ops/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,11 +223,6 @@ pub trait ChunkApply<'a, T> {
where
F: Fn(T) -> Self::FuncRet + Copy;

fn try_apply_values<F>(&'a self, f: F) -> PolarsResult<Self>
where
F: Fn(T) -> PolarsResult<Self::FuncRet> + Copy,
Self: Sized;

/// Apply a closure elementwise including null values.
#[must_use]
fn apply<F>(&'a self, f: F) -> Self
Expand Down

0 comments on commit 69ea11f

Please sign in to comment.