Add filter, map, reduce to Array.
This commit is contained in:
@@ -502,6 +502,13 @@ pub fn make_setter(id: &str) -> String {
|
||||
format!("{}{}", FN_SET, id)
|
||||
}
|
||||
|
||||
/// Is this function an anonymous function?
|
||||
#[cfg(not(feature = "no_function"))]
|
||||
#[inline(always)]
|
||||
pub fn is_anonymous_fn(fn_name: &str) -> bool {
|
||||
fn_name.starts_with(FN_ANONYMOUS)
|
||||
}
|
||||
|
||||
/// Print/debug to stdout
|
||||
fn default_print(_s: &str) {
|
||||
#[cfg(not(feature = "no_std"))]
|
||||
|
@@ -9,14 +9,11 @@ use crate::result::EvalAltResult;
|
||||
use crate::scope::Scope;
|
||||
use crate::token::{is_valid_identifier, Position};
|
||||
use crate::utils::ImmutableString;
|
||||
use crate::{calc_fn_hash, StaticVec};
|
||||
|
||||
#[cfg(not(feature = "no_function"))]
|
||||
use crate::{calc_fn_hash, module::FuncReturn, StaticVec};
|
||||
|
||||
use crate::stdlib::{boxed::Box, convert::TryFrom, fmt, string::String, vec::Vec};
|
||||
|
||||
#[cfg(not(feature = "no_function"))]
|
||||
use crate::stdlib::{iter::empty, mem};
|
||||
use crate::stdlib::{
|
||||
boxed::Box, convert::TryFrom, fmt, iter::empty, mem, string::String, vec::Vec,
|
||||
};
|
||||
|
||||
#[cfg(not(feature = "sync"))]
|
||||
use crate::stdlib::rc::Rc;
|
||||
@@ -114,14 +111,13 @@ impl FnPtr {
|
||||
/// This is to avoid unnecessarily cloning the arguments.
|
||||
/// Do not use the arguments after this call. If they are needed afterwards,
|
||||
/// clone them _before_ calling this function.
|
||||
#[cfg(not(feature = "no_function"))]
|
||||
pub fn call_dynamic(
|
||||
&self,
|
||||
engine: &Engine,
|
||||
lib: impl AsRef<Module>,
|
||||
this_ptr: Option<&mut Dynamic>,
|
||||
mut arg_values: impl AsMut<[Dynamic]>,
|
||||
) -> FuncReturn<Dynamic> {
|
||||
) -> Result<Dynamic, Box<EvalAltResult>> {
|
||||
let mut args_data = self
|
||||
.1
|
||||
.iter()
|
||||
|
@@ -14,10 +14,7 @@ use crate::{result::EvalAltResult, token::Position};
|
||||
#[cfg(not(feature = "no_object"))]
|
||||
use crate::engine::Map;
|
||||
|
||||
use crate::stdlib::{any::TypeId, boxed::Box};
|
||||
|
||||
#[cfg(not(feature = "unchecked"))]
|
||||
use crate::stdlib::string::ToString;
|
||||
use crate::stdlib::{any::TypeId, boxed::Box, string::ToString};
|
||||
|
||||
pub type Unit = ();
|
||||
|
||||
@@ -75,6 +72,10 @@ def_package!(crate:BasicArrayPackage:"Basic array utilities.", lib, {
|
||||
#[cfg(not(feature = "no_object"))]
|
||||
reg_functions!(lib += map; Map);
|
||||
|
||||
lib.set_raw_fn("map", &[TypeId::of::<Array>(), TypeId::of::<FnPtr>()], map);
|
||||
lib.set_raw_fn("filter", &[TypeId::of::<Array>(), TypeId::of::<FnPtr>()], filter);
|
||||
lib.set_raw_fn("reduce", &[TypeId::of::<Array>(), TypeId::of::<FnPtr>()], reduce);
|
||||
|
||||
// Merge in the module at the end to override `+=` for arrays
|
||||
combine_with_exported_module!(lib, "array", array_functions);
|
||||
|
||||
@@ -165,6 +166,109 @@ fn pad<T: Variant + Clone>(
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn map(
|
||||
engine: &Engine,
|
||||
lib: &Module,
|
||||
args: &mut [&mut Dynamic],
|
||||
) -> Result<Array, Box<EvalAltResult>> {
|
||||
let list = args[0].read_lock::<Array>().unwrap();
|
||||
let mapper = args[1].read_lock::<FnPtr>().unwrap();
|
||||
|
||||
let mut array = Array::with_capacity(list.len());
|
||||
|
||||
for (i, item) in list.iter().enumerate() {
|
||||
array.push(
|
||||
mapper
|
||||
.call_dynamic(engine, lib, None, [item.clone()])
|
||||
.or_else(|err| match *err {
|
||||
EvalAltResult::ErrorFunctionNotFound(_, _) => {
|
||||
mapper.call_dynamic(engine, lib, None, [item.clone(), (i as INT).into()])
|
||||
}
|
||||
_ => Err(err),
|
||||
})
|
||||
.map_err(|err| {
|
||||
Box::new(EvalAltResult::ErrorInFunctionCall(
|
||||
"map".to_string(),
|
||||
err,
|
||||
Position::none(),
|
||||
))
|
||||
})?,
|
||||
);
|
||||
}
|
||||
|
||||
Ok(array)
|
||||
}
|
||||
|
||||
fn filter(
|
||||
engine: &Engine,
|
||||
lib: &Module,
|
||||
args: &mut [&mut Dynamic],
|
||||
) -> Result<Array, Box<EvalAltResult>> {
|
||||
let list = args[0].read_lock::<Array>().unwrap();
|
||||
let filter = args[1].read_lock::<FnPtr>().unwrap();
|
||||
|
||||
let mut array = Array::with_capacity(list.len());
|
||||
|
||||
for (i, item) in list.iter().enumerate() {
|
||||
if filter
|
||||
.call_dynamic(engine, lib, None, [item.clone()])
|
||||
.or_else(|err| match *err {
|
||||
EvalAltResult::ErrorFunctionNotFound(_, _) => {
|
||||
filter.call_dynamic(engine, lib, None, [item.clone(), (i as INT).into()])
|
||||
}
|
||||
_ => Err(err),
|
||||
})
|
||||
.map_err(|err| {
|
||||
Box::new(EvalAltResult::ErrorInFunctionCall(
|
||||
"filter".to_string(),
|
||||
err,
|
||||
Position::none(),
|
||||
))
|
||||
})?
|
||||
.as_bool()
|
||||
.unwrap_or(false)
|
||||
{
|
||||
array.push(item.clone());
|
||||
}
|
||||
}
|
||||
|
||||
Ok(array)
|
||||
}
|
||||
|
||||
fn reduce(
|
||||
engine: &Engine,
|
||||
lib: &Module,
|
||||
args: &mut [&mut Dynamic],
|
||||
) -> Result<Dynamic, Box<EvalAltResult>> {
|
||||
let list = args[0].read_lock::<Array>().unwrap();
|
||||
let reducer = args[1].read_lock::<FnPtr>().unwrap();
|
||||
|
||||
let mut result: Dynamic = ().into();
|
||||
|
||||
for (i, item) in list.iter().enumerate() {
|
||||
result = reducer
|
||||
.call_dynamic(engine, lib, None, [result.clone(), item.clone()])
|
||||
.or_else(|err| match *err {
|
||||
EvalAltResult::ErrorFunctionNotFound(_, _) => reducer.call_dynamic(
|
||||
engine,
|
||||
lib,
|
||||
None,
|
||||
[result, item.clone(), (i as INT).into()],
|
||||
),
|
||||
_ => Err(err),
|
||||
})
|
||||
.map_err(|err| {
|
||||
Box::new(EvalAltResult::ErrorInFunctionCall(
|
||||
"reduce".to_string(),
|
||||
err,
|
||||
Position::none(),
|
||||
))
|
||||
})?;
|
||||
}
|
||||
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
gen_array_functions!(basic => INT, bool, char, ImmutableString, FnPtr, Array, Unit);
|
||||
|
||||
#[cfg(not(feature = "only_i32"))]
|
||||
|
@@ -5,6 +5,9 @@ use crate::error::ParseErrorType;
|
||||
use crate::parser::INT;
|
||||
use crate::token::Position;
|
||||
|
||||
#[cfg(not(feature = "no_function"))]
|
||||
use crate::engine::is_anonymous_fn;
|
||||
|
||||
use crate::stdlib::{
|
||||
boxed::Box,
|
||||
error::Error,
|
||||
@@ -166,6 +169,10 @@ impl fmt::Display for EvalAltResult {
|
||||
|
||||
Self::ErrorParsing(p, _) => write!(f, "Syntax error: {}", p)?,
|
||||
|
||||
#[cfg(not(feature = "no_function"))]
|
||||
Self::ErrorInFunctionCall(s, err, _) if is_anonymous_fn(s) => {
|
||||
write!(f, "Error in call to closure: {}", err)?
|
||||
}
|
||||
Self::ErrorInFunctionCall(s, err, _) => {
|
||||
write!(f, "Error in call to function '{}': {}", s, err)?
|
||||
}
|
||||
|
Reference in New Issue
Block a user