Add filter, map, reduce to Array.

This commit is contained in:
Stephen Chung 2020-10-12 22:49:51 +08:00
parent 6d0851de44
commit 747fda1ec7
6 changed files with 258 additions and 49 deletions

View File

@ -30,22 +30,25 @@ Built-in Functions
The following methods (mostly defined in the [`BasicArrayPackage`][packages] but excluded if using a [raw `Engine`]) operate on arrays:
| Function | Parameter(s) | Description |
| ------------------------- | -------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------- |
| `push` | element to insert | inserts an element at the end |
| `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) array to append | concatenates the second array to the end of the first |
| `+` operator | 1) first array<br/>2) second array | concatenates the first array with the second |
| `insert` | 1) element to insert<br/>2) position (beginning if <= 0, end if >= length) | inserts an element at a certain index |
| `pop` | _none_ | removes the last element and returns it ([`()`] if empty) |
| `shift` | _none_ | removes the first element and returns it ([`()`] if empty) |
| `remove` | index | removes an element at a particular index and returns it, or returns [`()`] if the index is not valid |
| `reverse` | _none_ | reverses the array |
| `len` method and property | _none_ | returns the number of elements |
| `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 |
| `truncate` | target length | cuts off the array at exactly a specified length (discarding all subsequent elements) |
| Function | Parameter(s) | Description |
| ------------------------- | ------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `push` | element to insert | inserts an element at the end |
| `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) array to append | concatenates the second array to the end of the first |
| `+` operator | 1) first array<br/>2) second array | concatenates the first array with the second |
| `insert` | 1) element to insert<br/>2) position (beginning if <= 0, end if >= length) | inserts an element at a certain index |
| `pop` | _none_ | removes the last element and returns it ([`()`] if empty) |
| `shift` | _none_ | removes the first element and returns it ([`()`] if empty) |
| `remove` | index | removes an element at a particular index and returns it, or returns [`()`] if the index is not valid |
| `reverse` | _none_ | reverses the array |
| `len` method and property | _none_ | returns the number of elements |
| `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 |
| `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
@ -63,12 +66,12 @@ Examples
--------
```rust
let y = [2, 3]; // array literal with 2 elements
let y = [2, 3]; // array literal with 2 elements
let y = [2, 3,]; // trailing comma is OK
let y = [2, 3,]; // trailing comma is OK
y.insert(0, 1); // insert element at the beginning
y.insert(999, 4); // insert element at the end
y.insert(0, 1); // insert element at the beginning
y.insert(999, 4); // insert element at the end
y.len == 4;
@ -77,21 +80,21 @@ y[1] == 2;
y[2] == 3;
y[3] == 4;
(1 in y) == true; // use 'in' to test if an item exists in the array
(42 in y) == false; // 'in' uses the '==' operator (which users can override)
// to check if the target item exists in the array
(1 in y) == true; // use 'in' to test if an item exists in the array
(42 in y) == false; // 'in' uses the '==' operator (which users can override)
// to check if the target item exists in the array
y[1] = 42; // array elements can be reassigned
y[1] = 42; // array elements can be reassigned
(42 in y) == true;
y.remove(2) == 3; // remove element
y.remove(2) == 3; // remove element
y.len == 3;
y[2] == 4; // elements after the removed element are shifted
y[2] == 4; // elements after the removed element are shifted
ts.list = y; // arrays can be assigned completely (by value copy)
ts.list = y; // arrays can be assigned completely (by value copy)
let foo = ts.list[1];
foo == 42;
@ -99,7 +102,7 @@ let foo = [1, 2, 3][0];
foo == 1;
fn abc() {
[42, 43, 44] // a function returning an array
[42, 43, 44] // a function returning an array
}
let foo = abc()[0];
@ -108,32 +111,51 @@ foo == 42;
let foo = y[0];
foo == 1;
y.push(4); // 4 elements
y += 5; // 5 elements
y.push(4); // 4 elements
y += 5; // 5 elements
y.len == 5;
let first = y.shift(); // remove the first element, 4 elements remaining
let first = y.shift(); // remove the first element, 4 elements remaining
first == 1;
let last = y.pop(); // remove the last element, 3 elements remaining
let last = y.pop(); // remove the last element, 3 elements remaining
last == 5;
y.len == 3;
for item in y { // arrays can be iterated with a 'for' statement
for item in y { // arrays can be iterated with a 'for' statement
print(item);
}
y.pad(10, "hello"); // pad the array up to 10 elements
y.pad(10, "hello"); // pad the array up to 10 elements
y.len == 10;
y.truncate(5); // truncate the array to 5 elements
y.truncate(5); // truncate the array to 5 elements
y.len == 5;
y.clear(); // empty the array
y.clear(); // empty the array
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;
```

View File

@ -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"))]

View File

@ -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()

View File

@ -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"))]

View File

@ -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)?
}

View File

@ -112,3 +112,76 @@ fn test_array_with_structs() -> Result<(), Box<EvalAltResult>> {
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(())
}