Add for_each for arrays.

This commit is contained in:
Stephen Chung 2022-12-25 12:44:49 +08:00
parent fd401f3048
commit 52d23522e6
4 changed files with 172 additions and 62 deletions

View File

@ -57,8 +57,9 @@ Net features
### Enhanced array API
* Array methods that take a function pointer, usually a closure (e.g. `map`, `filter`, `index_of` etc.), can now provide a closure with one few parameter but binds the first parameter to `this`.
* This vastly improves performance when working with arrays of object maps by avoiding unnecessary cloning of large types.
* Array methods that take a function pointer, usually a closure (e.g. `map`, `filter`, `index_of`, `reduce` etc.), can now bind the array element to `this` when calling a closure.
* This vastly improves performance when working with arrays of large types (e.g. object maps) by avoiding unnecessary cloning.
* `for_each` is also added for arrays, allowing a closure to mutate array elements (bound to `this`) in turn.
Enhancements
------------

View File

@ -644,6 +644,43 @@ pub mod array_functions {
result
}
}
/// Iterate through all the elements in the array, applying a `process` function to each element in turn.
/// Each element is bound to `this` before calling the function.
///
/// # Function Parameters
///
/// * `this`: bound to array element (mutable)
/// * `index` _(optional)_: current index in the array
///
/// # Example
///
/// ```rhai
/// let x = [1, 2, 3, 4, 5];
///
/// x.for_each(|| this *= this);
///
/// print(x); // prints "[1, 4, 9, 16, 25]"
///
/// x.for_each(|i| this *= i);
///
/// print(x); // prints "[0, 2, 6, 12, 20]"
/// ```
#[rhai_fn(return_raw)]
pub fn for_each(ctx: NativeCallContext, array: &mut Array, map: FnPtr) -> RhaiResultOf<()> {
if array.is_empty() {
return Ok(());
}
for (i, item) in array.iter_mut().enumerate() {
let ex = [(i as INT).into()];
let _ = map.call_raw_with_extra_args("map", &ctx, Some(item), [], ex, None)?;
}
Ok(())
}
/// Iterate through all the elements in the array, applying a `mapper` function to each element
/// in turn, and return the results as a new array.
///
@ -679,8 +716,7 @@ pub mod array_functions {
for (i, item) in array.iter_mut().enumerate() {
let ex = [(i as INT).into()];
ar.push(map.call_raw_with_extra_args("map", &ctx, Some(item), [], ex)?);
ar.push(map.call_raw_with_extra_args("map", &ctx, Some(item), [], ex, Some(0))?);
}
Ok(ar)
@ -723,7 +759,7 @@ pub mod array_functions {
let ex = [(i as INT).into()];
if filter
.call_raw_with_extra_args("filter", &ctx, Some(item), [], ex)?
.call_raw_with_extra_args("filter", &ctx, Some(item), [], ex, Some(0))?
.as_bool()
.unwrap_or(false)
{
@ -962,7 +998,7 @@ pub mod array_functions {
let ex = [(i as INT).into()];
if filter
.call_raw_with_extra_args("index_of", &ctx, Some(item), [], ex)?
.call_raw_with_extra_args("index_of", &ctx, Some(item), [], ex, Some(0))?
.as_bool()
.unwrap_or(false)
{
@ -1128,7 +1164,8 @@ pub mod array_functions {
for (i, item) in array.iter_mut().enumerate().skip(start) {
let ex = [(i as INT).into()];
let value = filter.call_raw_with_extra_args("find_map", &ctx, Some(item), [], ex)?;
let value =
filter.call_raw_with_extra_args("find_map", &ctx, Some(item), [], ex, Some(0))?;
if !value.is_unit() {
return Ok(value);
@ -1169,7 +1206,7 @@ pub mod array_functions {
let ex = [(i as INT).into()];
if filter
.call_raw_with_extra_args("some", &ctx, Some(item), [], ex)?
.call_raw_with_extra_args("some", &ctx, Some(item), [], ex, Some(0))?
.as_bool()
.unwrap_or(false)
{
@ -1211,7 +1248,7 @@ pub mod array_functions {
let ex = [(i as INT).into()];
if !filter
.call_raw_with_extra_args("all", &ctx, Some(item), [], ex)?
.call_raw_with_extra_args("all", &ctx, Some(item), [], ex, Some(0))?
.as_bool()
.unwrap_or(false)
{
@ -1334,11 +1371,11 @@ pub mod array_functions {
}
array
.iter()
.iter_mut()
.enumerate()
.try_fold(initial, |result, (i, item)| {
let ex = [(i as INT).into()];
reducer.call_raw_with_extra_args("reduce", &ctx, None, [result, item.clone()], ex)
reducer.call_raw_with_extra_args("reduce", &ctx, Some(item), [result], ex, Some(1))
})
}
/// Reduce an array by iterating through all elements, in _reverse_ order,
@ -1400,19 +1437,22 @@ pub mod array_functions {
return Ok(initial);
}
let len = array.len();
array
.iter()
.iter_mut()
.rev()
.enumerate()
.try_fold(initial, |result, (i, item)| {
let ex = [((array.len() - 1 - i) as INT).into()];
let ex = [((len - 1 - i) as INT).into()];
reducer.call_raw_with_extra_args(
"reduce_rev",
&ctx,
None,
[result, item.clone()],
Some(item),
[result],
ex,
Some(1),
)
})
}
@ -1602,7 +1642,7 @@ pub mod array_functions {
let ex = [(i as INT).into()];
if filter
.call_raw_with_extra_args("drain", &ctx, Some(&mut array[x]), [], ex)?
.call_raw_with_extra_args("drain", &ctx, Some(&mut array[x]), [], ex, Some(0))?
.as_bool()
.unwrap_or(false)
{
@ -1749,7 +1789,7 @@ pub mod array_functions {
let ex = [(i as INT).into()];
if filter
.call_raw_with_extra_args("retain", &ctx, Some(&mut array[x]), [], ex)?
.call_raw_with_extra_args("retain", &ctx, Some(&mut array[x]), [], ex, Some(0))?
.as_bool()
.unwrap_or(false)
{

View File

@ -360,7 +360,9 @@ impl FnPtr {
/// arguments attached.
///
/// If `this_ptr` is provided, it is first provided to script-defined functions bound to `this`.
/// When an appropriate function is not found, it is then removed and mapped to the first parameter.
///
/// When an appropriate function is not found and `move_this_ptr_to_args` is `Some`, `this_ptr`
/// is removed and inserted as the appropriate parameter number.
///
/// This is useful for calling predicate closures within an iteration loop where the extra argument
/// is the current element's index.
@ -374,17 +376,19 @@ impl FnPtr {
fn_name: &str,
ctx: &NativeCallContext,
this_ptr: Option<&mut Dynamic>,
items: [Dynamic; N],
args: [Dynamic; N],
extras: [Dynamic; E],
move_this_ptr_to_args: Option<usize>,
) -> RhaiResult {
self._call_with_extra_args(fn_name, ctx, this_ptr, items, extras)
self._call_with_extra_args(fn_name, ctx, this_ptr, args, extras, move_this_ptr_to_args)
}
/// _(internals)_ Make a call to a function pointer with either a specified number of arguments,
/// or with extra arguments attached.
/// Exported under the `internals` feature only.
/// or with extra arguments attached. Exported under the `internals` feature only.
///
/// If `this_ptr` is provided, it is first provided to script-defined functions bound to `this`.
/// When an appropriate function is not found, it is then removed and mapped to the first parameter.
///
/// When an appropriate function is not found and `move_this_ptr_to_args` is `Some`, `this_ptr`
/// is removed and inserted as the appropriate parameter number.
///
/// This is useful for calling predicate closures within an iteration loop where the extra
/// argument is the current element's index.
@ -398,10 +402,11 @@ impl FnPtr {
fn_name: &str,
ctx: &NativeCallContext,
this_ptr: Option<&mut Dynamic>,
items: [Dynamic; N],
args: [Dynamic; N],
extras: [Dynamic; E],
move_this_ptr_to_args: Option<usize>,
) -> RhaiResult {
self._call_with_extra_args(fn_name, ctx, this_ptr, items, extras)
self._call_with_extra_args(fn_name, ctx, this_ptr, args, extras, move_this_ptr_to_args)
}
/// Make a call to a function pointer with either a specified number of arguments, or with extra
/// arguments attached.
@ -410,51 +415,94 @@ impl FnPtr {
fn_name: &str,
ctx: &NativeCallContext,
mut this_ptr: Option<&mut Dynamic>,
items: [Dynamic; N],
args: [Dynamic; N],
extras: [Dynamic; E],
move_this_ptr_to_args: Option<usize>,
) -> RhaiResult {
#[cfg(not(feature = "no_function"))]
if let Some(arity) = self.fn_def().map(|f| f.params.len()) {
if arity == N + 1 && this_ptr.is_some() {
let mut args = FnArgsVec::with_capacity(items.len() + 1);
args.push(this_ptr.as_mut().unwrap().clone());
args.extend(items);
return self.call_raw(ctx, None, args);
if let Some(move_to_args) = move_this_ptr_to_args {
if this_ptr.is_some() {
if arity == N + 1 {
let mut args2 = FnArgsVec::with_capacity(args.len() + 1);
if move_to_args == 0 {
args2.push(this_ptr.as_mut().unwrap().clone());
args2.extend(args);
} else {
args2.extend(args);
args2.insert(move_to_args, this_ptr.as_mut().unwrap().clone());
}
return self.call_raw(ctx, None, args2);
}
if arity == N + E + 1 {
let mut args2 = FnArgsVec::with_capacity(args.len() + extras.len() + 1);
if move_to_args == 0 {
args2.push(this_ptr.as_mut().unwrap().clone());
args2.extend(args);
args2.extend(extras);
} else {
args2.extend(args);
args2.insert(move_to_args, this_ptr.as_mut().unwrap().clone());
args2.extend(extras);
}
return self.call_raw(ctx, None, args2);
}
}
}
if arity == N {
return self.call_raw(ctx, this_ptr, items);
return self.call_raw(ctx, this_ptr, args);
}
if arity == N + E {
let mut items2 = FnArgsVec::with_capacity(items.len() + extras.len());
items2.extend(IntoIterator::into_iter(items));
items2.extend(IntoIterator::into_iter(extras));
return self.call_raw(ctx, this_ptr, items2);
let mut args2 = FnArgsVec::with_capacity(args.len() + extras.len());
args2.extend(args);
args2.extend(extras);
return self.call_raw(ctx, this_ptr, args2);
}
}
self.call_raw(ctx, this_ptr.as_deref_mut(), items.clone())
self.call_raw(ctx, this_ptr.as_deref_mut(), args.clone())
.or_else(|err| match *err {
ERR::ErrorFunctionNotFound(sig, ..)
if this_ptr.is_some() && sig.starts_with(self.fn_name()) =>
if move_this_ptr_to_args.is_some()
&& this_ptr.is_some()
&& sig.starts_with(self.fn_name()) =>
{
let mut args = FnArgsVec::with_capacity(items.len() + 1);
args.push(this_ptr.as_mut().unwrap().clone());
args.extend(IntoIterator::into_iter(items.clone()));
self.call_raw(ctx, this_ptr.as_deref_mut(), args)
let mut args2 = FnArgsVec::with_capacity(args.len() + 1);
let move_to_args = move_this_ptr_to_args.unwrap();
if move_to_args == 0 {
args2.push(this_ptr.as_mut().unwrap().clone());
args2.extend(args.clone());
} else {
args2.extend(args.clone());
args2.insert(move_to_args, this_ptr.as_mut().unwrap().clone());
}
self.call_raw(ctx, None, args2)
}
_ => Err(err),
})
.or_else(|err| match *err {
ERR::ErrorFunctionNotFound(sig, ..) if sig.starts_with(self.fn_name()) => {
let mut args = FnArgsVec::with_capacity(
items.len() + extras.len() + if this_ptr.is_some() { 1 } else { 0 },
);
if let Some(move_to_args) = move_this_ptr_to_args {
if let Some(ref mut this_ptr) = this_ptr {
args.push(this_ptr.clone());
let mut args2 = FnArgsVec::with_capacity(args.len() + extras.len() + 1);
if move_to_args == 0 {
args2.push(this_ptr.clone());
args2.extend(args);
args2.extend(extras);
} else {
args2.extend(args);
args2.extend(extras);
args2.insert(move_to_args, this_ptr.clone());
}
args.extend(IntoIterator::into_iter(items));
args.extend(IntoIterator::into_iter(extras));
self.call_raw(ctx, this_ptr, args)
return self.call_raw(ctx, None, args2);
}
}
let mut args2 = FnArgsVec::with_capacity(args.len() + extras.len());
args2.extend(args);
args2.extend(extras);
self.call_raw(ctx, this_ptr, args2)
}
_ => Err(err),
})

View File

@ -299,11 +299,19 @@ fn test_arrays_map_reduce() -> Result<(), Box<EvalAltResult>> {
assert_eq!(engine.eval::<INT>("[1].map(|x| x + 41)[0]")?, 42);
assert_eq!(engine.eval::<INT>("[1].map(|| this + 41)[0]")?, 42);
assert_eq!(
engine.eval::<INT>("let x = [1, 2, 3]; x.for_each(|| this += 41); x[0]")?,
42
);
assert_eq!(engine.eval::<INT>("([1].map(|x| x + 41))[0]")?, 42);
assert_eq!(
engine.eval::<INT>("let c = 40; let y = 1; [1].map(|x, i| c + x + y + i)[0]")?,
42
);
assert_eq!(
engine.eval::<INT>("let x = [1, 2, 3]; x.for_each(|i| this += i); x[2]")?,
5
);
assert_eq!(
engine
@ -387,18 +395,31 @@ fn test_arrays_map_reduce() -> Result<(), Box<EvalAltResult>> {
14
);
assert_eq!(
engine.eval::<INT>(
"
let x = [1, 2, 3];
x.reduce(|sum, v, i| {
if i == 0 { sum = 10 }
sum + v * v
})
"
)?,
24
);
// assert_eq!(
// engine.eval::<INT>(
// "
// let x = [1, 2, 3];
// x.reduce(|sum, v, i| {
// if i == 0 { sum = 10 }
// sum + v * v
// })
// "
// )?,
// 24
// );
// assert_eq!(
// engine.eval::<INT>(
// "
// let x = [1, 2, 3];
// x.reduce(|sum, i| {
// if i == 0 { sum = 10 }
// sum + this * this
// })
// "
// )?,
// 24
// );
assert_eq!(
engine.eval::<INT>(