use crate::builtin::{Array, Callable, VarArray, Variant, to_usize};
use crate::meta::{AsArg, Element};
use crate::{meta, sys};
pub struct ArrayFunctionalOps<'a, T: Element> {
array: &'a Array<T>,
}
impl<'a, T: Element> ArrayFunctionalOps<'a, T> {
pub(super) fn new(owner: &'a Array<T>) -> Self {
Self { array: owner }
}
#[must_use]
pub fn filter(&self, callable: &Callable) -> Array<T> {
self.array.as_inner().filter(callable).cast_array::<T>()
}
#[must_use]
pub fn map(&self, callable: &Callable) -> VarArray {
self.array.as_inner().map(callable).cast_array()
}
#[must_use]
pub fn reduce(&self, callable: &Callable, initial: &Variant) -> Variant {
self.array.as_inner().reduce(callable, initial)
}
pub fn any(&self, callable: &Callable) -> bool {
self.array.as_inner().any(callable)
}
pub fn all(&self, callable: &Callable) -> bool {
self.array.as_inner().all(callable)
}
#[cfg(since_api = "4.4")] #[cfg_attr(published_docs, doc(cfg(since_api = "4.4")))]
pub fn find_custom(&self, callable: &Callable, from: Option<usize>) -> Option<usize> {
let from = from.map(|i| i as i64).unwrap_or(0);
let found_index = self.array.as_inner().find_custom(callable, from);
sys::found_to_option(found_index)
}
#[cfg(since_api = "4.4")] #[cfg_attr(published_docs, doc(cfg(since_api = "4.4")))]
pub fn rfind_custom(&self, callable: &Callable, from: Option<usize>) -> Option<usize> {
let from = from.map(|i| i as i64).unwrap_or(-1);
let found_index = self.array.as_inner().rfind_custom(callable, from);
sys::found_to_option(found_index)
}
pub fn bsearch_custom(&self, value: impl AsArg<T>, pred: &Callable) -> usize {
meta::arg_into_ref!(value: T);
to_usize(
self.array
.as_inner()
.bsearch_custom(&value.to_variant(), pred, true),
)
}
}