use super::*;
use crate::prelude::PlanCallback;
pub fn fold_exprs<E>(
acc: Expr,
f: PlanCallback<(Series, Series), Series>,
exprs: E,
returns_scalar: bool,
return_dtype: Option<DataTypeExpr>,
) -> Expr
where
E: AsRef<[Expr]>,
{
let mut exprs_v = Vec::with_capacity(exprs.as_ref().len() + 1);
exprs_v.push(acc);
exprs_v.extend(exprs.as_ref().iter().cloned());
Expr::Function {
input: exprs_v,
function: FunctionExpr::FoldHorizontal {
callback: f,
returns_scalar,
return_dtype,
},
}
}
pub fn reduce_exprs<E>(
f: PlanCallback<(Series, Series), Series>,
exprs: E,
returns_scalar: bool,
return_dtype: Option<DataTypeExpr>,
) -> Expr
where
E: AsRef<[Expr]>,
{
let exprs = exprs.as_ref().to_vec();
Expr::Function {
input: exprs,
function: FunctionExpr::ReduceHorizontal {
callback: f,
returns_scalar,
return_dtype,
},
}
}
#[cfg(feature = "dtype-struct")]
pub fn cum_reduce_exprs<E>(
f: PlanCallback<(Series, Series), Series>,
exprs: E,
returns_scalar: bool,
return_dtype: Option<DataTypeExpr>,
) -> Expr
where
E: AsRef<[Expr]>,
{
let exprs = exprs.as_ref().to_vec();
Expr::Function {
input: exprs,
function: FunctionExpr::CumReduceHorizontal {
callback: f,
returns_scalar,
return_dtype,
},
}
}
#[cfg(feature = "dtype-struct")]
pub fn cum_fold_exprs<E>(
acc: Expr,
f: PlanCallback<(Series, Series), Series>,
exprs: E,
returns_scalar: bool,
return_dtype: Option<DataTypeExpr>,
include_init: bool,
) -> Expr
where
E: AsRef<[Expr]>,
{
let exprs = exprs.as_ref();
let mut exprs_v = Vec::with_capacity(exprs.len());
exprs_v.push(acc);
exprs_v.extend(exprs.iter().cloned());
Expr::Function {
input: exprs_v,
function: FunctionExpr::CumFoldHorizontal {
callback: f,
returns_scalar,
return_dtype,
include_init,
},
}
}
pub fn all_horizontal<E: AsRef<[Expr]>>(exprs: E) -> PolarsResult<Expr> {
let exprs = exprs.as_ref().to_vec();
polars_ensure!(!exprs.is_empty(), ComputeError: "cannot return empty fold because the number of output rows is unknown");
Ok(Expr::n_ary(
FunctionExpr::Boolean(BooleanFunction::AllHorizontal),
exprs,
))
}
pub fn any_horizontal<E: AsRef<[Expr]>>(exprs: E) -> PolarsResult<Expr> {
let exprs = exprs.as_ref().to_vec();
polars_ensure!(!exprs.is_empty(), ComputeError: "cannot return empty fold because the number of output rows is unknown");
Ok(Expr::n_ary(
FunctionExpr::Boolean(BooleanFunction::AnyHorizontal),
exprs,
))
}
pub fn max_horizontal<E: AsRef<[Expr]>>(exprs: E) -> PolarsResult<Expr> {
let exprs = exprs.as_ref().to_vec();
polars_ensure!(!exprs.is_empty(), ComputeError: "cannot return empty fold because the number of output rows is unknown");
Ok(Expr::n_ary(FunctionExpr::MaxHorizontal, exprs))
}
pub fn min_horizontal<E: AsRef<[Expr]>>(exprs: E) -> PolarsResult<Expr> {
let exprs = exprs.as_ref().to_vec();
polars_ensure!(!exprs.is_empty(), ComputeError: "cannot return empty fold because the number of output rows is unknown");
Ok(Expr::n_ary(FunctionExpr::MinHorizontal, exprs))
}
pub fn sum_horizontal<E: AsRef<[Expr]>>(exprs: E, ignore_nulls: bool) -> PolarsResult<Expr> {
let exprs = exprs.as_ref().to_vec();
polars_ensure!(!exprs.is_empty(), ComputeError: "cannot return empty fold because the number of output rows is unknown");
Ok(Expr::n_ary(
FunctionExpr::SumHorizontal { ignore_nulls },
exprs,
))
}
pub fn mean_horizontal<E: AsRef<[Expr]>>(exprs: E, ignore_nulls: bool) -> PolarsResult<Expr> {
let exprs = exprs.as_ref().to_vec();
polars_ensure!(!exprs.is_empty(), ComputeError: "cannot return empty fold because the number of output rows is unknown");
Ok(Expr::n_ary(
FunctionExpr::MeanHorizontal { ignore_nulls },
exprs,
))
}
pub fn coalesce(exprs: &[Expr]) -> Expr {
Expr::n_ary(FunctionExpr::Coalesce, exprs.to_vec())
}