Add filter, map, reduce to Array.
This commit is contained in:
parent
6d0851de44
commit
747fda1ec7
@ -31,7 +31,7 @@ Built-in Functions
|
|||||||
The following methods (mostly defined in the [`BasicArrayPackage`][packages] but excluded if using a [raw `Engine`]) operate on arrays:
|
The following methods (mostly defined in the [`BasicArrayPackage`][packages] but excluded if using a [raw `Engine`]) operate on arrays:
|
||||||
|
|
||||||
| Function | Parameter(s) | Description |
|
| Function | Parameter(s) | Description |
|
||||||
| ------------------------- | -------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------- |
|
| ------------------------- | ------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||||
| `push` | element to insert | inserts an element at the end |
|
| `push` | element to insert | inserts an element at the end |
|
||||||
| `append` | array to append | concatenates the second array to the end of the first |
|
| `append` | array to append | concatenates the second array to the end of the first |
|
||||||
| `+=` operator | 1) array<br/>2) element to insert (not another array) | inserts an element at the end |
|
| `+=` operator | 1) array<br/>2) element to insert (not another array) | inserts an element at the end |
|
||||||
@ -46,6 +46,9 @@ The following methods (mostly defined in the [`BasicArrayPackage`][packages] but
|
|||||||
| `pad` | 1) target length<br/>2) element to pad | pads the array with an element to at least a specified length |
|
| `pad` | 1) target length<br/>2) element to pad | pads the array with an element to at least a specified length |
|
||||||
| `clear` | _none_ | empties the array |
|
| `clear` | _none_ | empties the array |
|
||||||
| `truncate` | target length | cuts off the array at exactly a specified length (discarding all subsequent elements) |
|
| `truncate` | target length | cuts off the array at exactly a specified length (discarding all subsequent elements) |
|
||||||
|
| `filter` | 1) array<br/>2) [function pointer] to predicate (can be a [closure]) | constructs a new array with all items that returns `true` when called with the predicate function:<br/>1st parameter: array item,<br/>2nd parameter: offset index (optional) |
|
||||||
|
| `map` | 1) array<br/>2) [function pointer] to conversion function (can be a [closure]) | constructs a new array with all items mapped to the result of applying the conversion function:<br/>1st parameter: array item,<br/>2nd parameter: offset index (optional) |
|
||||||
|
| `reduce` | 1) array<br/>2) [function pointer] to accumulator function (can be a [closure]) | constructs a new array with all items accumulated by the accumulator function:<br/>1st parameter: accumulated value ([`()`] initially),<br/>2nd parameter: array item,<br/>3rd parameter: offset index (optional) |
|
||||||
|
|
||||||
|
|
||||||
Use Custom Types With Arrays
|
Use Custom Types With Arrays
|
||||||
@ -136,4 +139,23 @@ y.len == 5;
|
|||||||
y.clear(); // empty the array
|
y.clear(); // empty the array
|
||||||
|
|
||||||
y.len == 0;
|
y.len == 0;
|
||||||
|
|
||||||
|
let a = [42, 123, 99];
|
||||||
|
|
||||||
|
a.map(|v| v + 1); // [43, 124, 100]
|
||||||
|
|
||||||
|
a.map(|v, i| v + i); // [42, 124, 101]
|
||||||
|
|
||||||
|
a.filter(|v| v > 50); // [123, 99]
|
||||||
|
|
||||||
|
a.filter(|v, i| i == 1); // [123]
|
||||||
|
|
||||||
|
a.reduce(|sum, v| {
|
||||||
|
// Detect the initial value of '()'
|
||||||
|
if sum.type_of() == "()" { v } else { sum + v }
|
||||||
|
) == 264;
|
||||||
|
|
||||||
|
a.reduce(|sum, v, i| {
|
||||||
|
if i == 0 { v } else { sum + v }
|
||||||
|
) == 264;
|
||||||
```
|
```
|
||||||
|
@ -502,6 +502,13 @@ pub fn make_setter(id: &str) -> String {
|
|||||||
format!("{}{}", FN_SET, id)
|
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
|
/// Print/debug to stdout
|
||||||
fn default_print(_s: &str) {
|
fn default_print(_s: &str) {
|
||||||
#[cfg(not(feature = "no_std"))]
|
#[cfg(not(feature = "no_std"))]
|
||||||
|
@ -9,14 +9,11 @@ use crate::result::EvalAltResult;
|
|||||||
use crate::scope::Scope;
|
use crate::scope::Scope;
|
||||||
use crate::token::{is_valid_identifier, Position};
|
use crate::token::{is_valid_identifier, Position};
|
||||||
use crate::utils::ImmutableString;
|
use crate::utils::ImmutableString;
|
||||||
|
use crate::{calc_fn_hash, StaticVec};
|
||||||
|
|
||||||
#[cfg(not(feature = "no_function"))]
|
use crate::stdlib::{
|
||||||
use crate::{calc_fn_hash, module::FuncReturn, StaticVec};
|
boxed::Box, convert::TryFrom, fmt, iter::empty, mem, string::String, vec::Vec,
|
||||||
|
};
|
||||||
use crate::stdlib::{boxed::Box, convert::TryFrom, fmt, string::String, vec::Vec};
|
|
||||||
|
|
||||||
#[cfg(not(feature = "no_function"))]
|
|
||||||
use crate::stdlib::{iter::empty, mem};
|
|
||||||
|
|
||||||
#[cfg(not(feature = "sync"))]
|
#[cfg(not(feature = "sync"))]
|
||||||
use crate::stdlib::rc::Rc;
|
use crate::stdlib::rc::Rc;
|
||||||
@ -114,14 +111,13 @@ impl FnPtr {
|
|||||||
/// This is to avoid unnecessarily cloning the arguments.
|
/// This is to avoid unnecessarily cloning the arguments.
|
||||||
/// Do not use the arguments after this call. If they are needed afterwards,
|
/// Do not use the arguments after this call. If they are needed afterwards,
|
||||||
/// clone them _before_ calling this function.
|
/// clone them _before_ calling this function.
|
||||||
#[cfg(not(feature = "no_function"))]
|
|
||||||
pub fn call_dynamic(
|
pub fn call_dynamic(
|
||||||
&self,
|
&self,
|
||||||
engine: &Engine,
|
engine: &Engine,
|
||||||
lib: impl AsRef<Module>,
|
lib: impl AsRef<Module>,
|
||||||
this_ptr: Option<&mut Dynamic>,
|
this_ptr: Option<&mut Dynamic>,
|
||||||
mut arg_values: impl AsMut<[Dynamic]>,
|
mut arg_values: impl AsMut<[Dynamic]>,
|
||||||
) -> FuncReturn<Dynamic> {
|
) -> Result<Dynamic, Box<EvalAltResult>> {
|
||||||
let mut args_data = self
|
let mut args_data = self
|
||||||
.1
|
.1
|
||||||
.iter()
|
.iter()
|
||||||
|
@ -14,10 +14,7 @@ use crate::{result::EvalAltResult, token::Position};
|
|||||||
#[cfg(not(feature = "no_object"))]
|
#[cfg(not(feature = "no_object"))]
|
||||||
use crate::engine::Map;
|
use crate::engine::Map;
|
||||||
|
|
||||||
use crate::stdlib::{any::TypeId, boxed::Box};
|
use crate::stdlib::{any::TypeId, boxed::Box, string::ToString};
|
||||||
|
|
||||||
#[cfg(not(feature = "unchecked"))]
|
|
||||||
use crate::stdlib::string::ToString;
|
|
||||||
|
|
||||||
pub type Unit = ();
|
pub type Unit = ();
|
||||||
|
|
||||||
@ -75,6 +72,10 @@ def_package!(crate:BasicArrayPackage:"Basic array utilities.", lib, {
|
|||||||
#[cfg(not(feature = "no_object"))]
|
#[cfg(not(feature = "no_object"))]
|
||||||
reg_functions!(lib += map; Map);
|
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
|
// Merge in the module at the end to override `+=` for arrays
|
||||||
combine_with_exported_module!(lib, "array", array_functions);
|
combine_with_exported_module!(lib, "array", array_functions);
|
||||||
|
|
||||||
@ -165,6 +166,109 @@ fn pad<T: Variant + Clone>(
|
|||||||
Ok(())
|
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);
|
gen_array_functions!(basic => INT, bool, char, ImmutableString, FnPtr, Array, Unit);
|
||||||
|
|
||||||
#[cfg(not(feature = "only_i32"))]
|
#[cfg(not(feature = "only_i32"))]
|
||||||
|
@ -5,6 +5,9 @@ use crate::error::ParseErrorType;
|
|||||||
use crate::parser::INT;
|
use crate::parser::INT;
|
||||||
use crate::token::Position;
|
use crate::token::Position;
|
||||||
|
|
||||||
|
#[cfg(not(feature = "no_function"))]
|
||||||
|
use crate::engine::is_anonymous_fn;
|
||||||
|
|
||||||
use crate::stdlib::{
|
use crate::stdlib::{
|
||||||
boxed::Box,
|
boxed::Box,
|
||||||
error::Error,
|
error::Error,
|
||||||
@ -166,6 +169,10 @@ impl fmt::Display for EvalAltResult {
|
|||||||
|
|
||||||
Self::ErrorParsing(p, _) => write!(f, "Syntax error: {}", p)?,
|
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, _) => {
|
Self::ErrorInFunctionCall(s, err, _) => {
|
||||||
write!(f, "Error in call to function '{}': {}", s, err)?
|
write!(f, "Error in call to function '{}': {}", s, err)?
|
||||||
}
|
}
|
||||||
|
@ -112,3 +112,76 @@ fn test_array_with_structs() -> Result<(), Box<EvalAltResult>> {
|
|||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(not(feature = "no_object"))]
|
||||||
|
#[cfg(not(feature = "no_function"))]
|
||||||
|
#[cfg(not(feature = "no_closure"))]
|
||||||
|
#[test]
|
||||||
|
fn test_arrays_map_reduce() -> Result<(), Box<EvalAltResult>> {
|
||||||
|
let engine = Engine::new();
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
engine.eval::<INT>(
|
||||||
|
r"
|
||||||
|
let x = [1, 2, 3];
|
||||||
|
let y = x.filter(|v| v > 2);
|
||||||
|
y[0]
|
||||||
|
"
|
||||||
|
)?,
|
||||||
|
3
|
||||||
|
);
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
engine.eval::<INT>(
|
||||||
|
r"
|
||||||
|
let x = [1, 2, 3];
|
||||||
|
let y = x.filter(|v, i| v > i);
|
||||||
|
y.len()
|
||||||
|
"
|
||||||
|
)?,
|
||||||
|
3
|
||||||
|
);
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
engine.eval::<INT>(
|
||||||
|
r"
|
||||||
|
let x = [1, 2, 3];
|
||||||
|
let y = x.map(|v| v * 2);
|
||||||
|
y[2]
|
||||||
|
"
|
||||||
|
)?,
|
||||||
|
6
|
||||||
|
);
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
engine.eval::<INT>(
|
||||||
|
r"
|
||||||
|
let x = [1, 2, 3];
|
||||||
|
let y = x.map(|v, i| v * i);
|
||||||
|
y[2]
|
||||||
|
"
|
||||||
|
)?,
|
||||||
|
6
|
||||||
|
);
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
engine.eval::<INT>(
|
||||||
|
r#"
|
||||||
|
let x = [1, 2, 3];
|
||||||
|
x.reduce(|sum, v| if sum.type_of() == "()" { v } else { sum + v * v })
|
||||||
|
"#
|
||||||
|
)?,
|
||||||
|
14
|
||||||
|
);
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
engine.eval::<INT>(
|
||||||
|
r#"
|
||||||
|
let x = [1, 2, 3];
|
||||||
|
x.reduce(|sum, v, i| { if i==0 { sum = 10 } sum + v * v })
|
||||||
|
"#
|
||||||
|
)?,
|
||||||
|
24
|
||||||
|
);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user