Add for_each for arrays.
This commit is contained in:
parent
fd401f3048
commit
52d23522e6
@ -57,8 +57,9 @@ Net features
|
|||||||
|
|
||||||
### Enhanced array API
|
### 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`.
|
* 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 object maps by avoiding unnecessary cloning of large types.
|
* 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
|
Enhancements
|
||||||
------------
|
------------
|
||||||
|
@ -644,6 +644,43 @@ pub mod array_functions {
|
|||||||
result
|
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
|
/// 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.
|
/// 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() {
|
for (i, item) in array.iter_mut().enumerate() {
|
||||||
let ex = [(i as INT).into()];
|
let ex = [(i as INT).into()];
|
||||||
|
ar.push(map.call_raw_with_extra_args("map", &ctx, Some(item), [], ex, Some(0))?);
|
||||||
ar.push(map.call_raw_with_extra_args("map", &ctx, Some(item), [], ex)?);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(ar)
|
Ok(ar)
|
||||||
@ -723,7 +759,7 @@ pub mod array_functions {
|
|||||||
let ex = [(i as INT).into()];
|
let ex = [(i as INT).into()];
|
||||||
|
|
||||||
if filter
|
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()
|
.as_bool()
|
||||||
.unwrap_or(false)
|
.unwrap_or(false)
|
||||||
{
|
{
|
||||||
@ -962,7 +998,7 @@ pub mod array_functions {
|
|||||||
let ex = [(i as INT).into()];
|
let ex = [(i as INT).into()];
|
||||||
|
|
||||||
if filter
|
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()
|
.as_bool()
|
||||||
.unwrap_or(false)
|
.unwrap_or(false)
|
||||||
{
|
{
|
||||||
@ -1128,7 +1164,8 @@ pub mod array_functions {
|
|||||||
for (i, item) in array.iter_mut().enumerate().skip(start) {
|
for (i, item) in array.iter_mut().enumerate().skip(start) {
|
||||||
let ex = [(i as INT).into()];
|
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() {
|
if !value.is_unit() {
|
||||||
return Ok(value);
|
return Ok(value);
|
||||||
@ -1169,7 +1206,7 @@ pub mod array_functions {
|
|||||||
let ex = [(i as INT).into()];
|
let ex = [(i as INT).into()];
|
||||||
|
|
||||||
if filter
|
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()
|
.as_bool()
|
||||||
.unwrap_or(false)
|
.unwrap_or(false)
|
||||||
{
|
{
|
||||||
@ -1211,7 +1248,7 @@ pub mod array_functions {
|
|||||||
let ex = [(i as INT).into()];
|
let ex = [(i as INT).into()];
|
||||||
|
|
||||||
if !filter
|
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()
|
.as_bool()
|
||||||
.unwrap_or(false)
|
.unwrap_or(false)
|
||||||
{
|
{
|
||||||
@ -1334,11 +1371,11 @@ pub mod array_functions {
|
|||||||
}
|
}
|
||||||
|
|
||||||
array
|
array
|
||||||
.iter()
|
.iter_mut()
|
||||||
.enumerate()
|
.enumerate()
|
||||||
.try_fold(initial, |result, (i, item)| {
|
.try_fold(initial, |result, (i, item)| {
|
||||||
let ex = [(i as INT).into()];
|
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,
|
/// Reduce an array by iterating through all elements, in _reverse_ order,
|
||||||
@ -1400,19 +1437,22 @@ pub mod array_functions {
|
|||||||
return Ok(initial);
|
return Ok(initial);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let len = array.len();
|
||||||
|
|
||||||
array
|
array
|
||||||
.iter()
|
.iter_mut()
|
||||||
.rev()
|
.rev()
|
||||||
.enumerate()
|
.enumerate()
|
||||||
.try_fold(initial, |result, (i, item)| {
|
.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(
|
reducer.call_raw_with_extra_args(
|
||||||
"reduce_rev",
|
"reduce_rev",
|
||||||
&ctx,
|
&ctx,
|
||||||
None,
|
Some(item),
|
||||||
[result, item.clone()],
|
[result],
|
||||||
ex,
|
ex,
|
||||||
|
Some(1),
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@ -1602,7 +1642,7 @@ pub mod array_functions {
|
|||||||
let ex = [(i as INT).into()];
|
let ex = [(i as INT).into()];
|
||||||
|
|
||||||
if filter
|
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()
|
.as_bool()
|
||||||
.unwrap_or(false)
|
.unwrap_or(false)
|
||||||
{
|
{
|
||||||
@ -1749,7 +1789,7 @@ pub mod array_functions {
|
|||||||
let ex = [(i as INT).into()];
|
let ex = [(i as INT).into()];
|
||||||
|
|
||||||
if filter
|
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()
|
.as_bool()
|
||||||
.unwrap_or(false)
|
.unwrap_or(false)
|
||||||
{
|
{
|
||||||
|
@ -360,7 +360,9 @@ impl FnPtr {
|
|||||||
/// arguments attached.
|
/// arguments attached.
|
||||||
///
|
///
|
||||||
/// If `this_ptr` is provided, it is first provided to script-defined functions bound to `this`.
|
/// 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
|
/// This is useful for calling predicate closures within an iteration loop where the extra argument
|
||||||
/// is the current element's index.
|
/// is the current element's index.
|
||||||
@ -374,17 +376,19 @@ impl FnPtr {
|
|||||||
fn_name: &str,
|
fn_name: &str,
|
||||||
ctx: &NativeCallContext,
|
ctx: &NativeCallContext,
|
||||||
this_ptr: Option<&mut Dynamic>,
|
this_ptr: Option<&mut Dynamic>,
|
||||||
items: [Dynamic; N],
|
args: [Dynamic; N],
|
||||||
extras: [Dynamic; E],
|
extras: [Dynamic; E],
|
||||||
|
move_this_ptr_to_args: Option<usize>,
|
||||||
) -> RhaiResult {
|
) -> 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,
|
/// _(internals)_ Make a call to a function pointer with either a specified number of arguments,
|
||||||
/// or with extra arguments attached.
|
/// or with extra arguments attached. Exported under the `internals` feature only.
|
||||||
/// Exported under the `internals` feature only.
|
|
||||||
///
|
///
|
||||||
/// If `this_ptr` is provided, it is first provided to script-defined functions bound to `this`.
|
/// 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
|
/// This is useful for calling predicate closures within an iteration loop where the extra
|
||||||
/// argument is the current element's index.
|
/// argument is the current element's index.
|
||||||
@ -398,10 +402,11 @@ impl FnPtr {
|
|||||||
fn_name: &str,
|
fn_name: &str,
|
||||||
ctx: &NativeCallContext,
|
ctx: &NativeCallContext,
|
||||||
this_ptr: Option<&mut Dynamic>,
|
this_ptr: Option<&mut Dynamic>,
|
||||||
items: [Dynamic; N],
|
args: [Dynamic; N],
|
||||||
extras: [Dynamic; E],
|
extras: [Dynamic; E],
|
||||||
|
move_this_ptr_to_args: Option<usize>,
|
||||||
) -> RhaiResult {
|
) -> 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
|
/// Make a call to a function pointer with either a specified number of arguments, or with extra
|
||||||
/// arguments attached.
|
/// arguments attached.
|
||||||
@ -410,51 +415,94 @@ impl FnPtr {
|
|||||||
fn_name: &str,
|
fn_name: &str,
|
||||||
ctx: &NativeCallContext,
|
ctx: &NativeCallContext,
|
||||||
mut this_ptr: Option<&mut Dynamic>,
|
mut this_ptr: Option<&mut Dynamic>,
|
||||||
items: [Dynamic; N],
|
args: [Dynamic; N],
|
||||||
extras: [Dynamic; E],
|
extras: [Dynamic; E],
|
||||||
|
move_this_ptr_to_args: Option<usize>,
|
||||||
) -> RhaiResult {
|
) -> RhaiResult {
|
||||||
#[cfg(not(feature = "no_function"))]
|
#[cfg(not(feature = "no_function"))]
|
||||||
if let Some(arity) = self.fn_def().map(|f| f.params.len()) {
|
if let Some(arity) = self.fn_def().map(|f| f.params.len()) {
|
||||||
if arity == N + 1 && this_ptr.is_some() {
|
if let Some(move_to_args) = move_this_ptr_to_args {
|
||||||
let mut args = FnArgsVec::with_capacity(items.len() + 1);
|
if this_ptr.is_some() {
|
||||||
args.push(this_ptr.as_mut().unwrap().clone());
|
if arity == N + 1 {
|
||||||
args.extend(items);
|
let mut args2 = FnArgsVec::with_capacity(args.len() + 1);
|
||||||
return self.call_raw(ctx, None, args);
|
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 {
|
if arity == N {
|
||||||
return self.call_raw(ctx, this_ptr, items);
|
return self.call_raw(ctx, this_ptr, args);
|
||||||
}
|
}
|
||||||
if arity == N + E {
|
if arity == N + E {
|
||||||
let mut items2 = FnArgsVec::with_capacity(items.len() + extras.len());
|
let mut args2 = FnArgsVec::with_capacity(args.len() + extras.len());
|
||||||
items2.extend(IntoIterator::into_iter(items));
|
args2.extend(args);
|
||||||
items2.extend(IntoIterator::into_iter(extras));
|
args2.extend(extras);
|
||||||
return self.call_raw(ctx, this_ptr, items2);
|
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 {
|
.or_else(|err| match *err {
|
||||||
ERR::ErrorFunctionNotFound(sig, ..)
|
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);
|
let mut args2 = FnArgsVec::with_capacity(args.len() + 1);
|
||||||
args.push(this_ptr.as_mut().unwrap().clone());
|
let move_to_args = move_this_ptr_to_args.unwrap();
|
||||||
args.extend(IntoIterator::into_iter(items.clone()));
|
if move_to_args == 0 {
|
||||||
self.call_raw(ctx, this_ptr.as_deref_mut(), args)
|
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),
|
_ => Err(err),
|
||||||
})
|
})
|
||||||
.or_else(|err| match *err {
|
.or_else(|err| match *err {
|
||||||
ERR::ErrorFunctionNotFound(sig, ..) if sig.starts_with(self.fn_name()) => {
|
ERR::ErrorFunctionNotFound(sig, ..) if sig.starts_with(self.fn_name()) => {
|
||||||
let mut args = FnArgsVec::with_capacity(
|
if let Some(move_to_args) = move_this_ptr_to_args {
|
||||||
items.len() + extras.len() + if this_ptr.is_some() { 1 } else { 0 },
|
if let Some(ref mut this_ptr) = this_ptr {
|
||||||
);
|
let mut args2 = FnArgsVec::with_capacity(args.len() + extras.len() + 1);
|
||||||
if let Some(ref mut this_ptr) = this_ptr {
|
if move_to_args == 0 {
|
||||||
args.push(this_ptr.clone());
|
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());
|
||||||
|
}
|
||||||
|
return self.call_raw(ctx, None, args2);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
args.extend(IntoIterator::into_iter(items));
|
|
||||||
args.extend(IntoIterator::into_iter(extras));
|
let mut args2 = FnArgsVec::with_capacity(args.len() + extras.len());
|
||||||
self.call_raw(ctx, this_ptr, args)
|
args2.extend(args);
|
||||||
|
args2.extend(extras);
|
||||||
|
|
||||||
|
self.call_raw(ctx, this_ptr, args2)
|
||||||
}
|
}
|
||||||
_ => Err(err),
|
_ => Err(err),
|
||||||
})
|
})
|
||||||
|
@ -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(|x| x + 41)[0]")?, 42);
|
||||||
assert_eq!(engine.eval::<INT>("[1].map(|| this + 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>("([1].map(|x| x + 41))[0]")?, 42);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
engine.eval::<INT>("let c = 40; let y = 1; [1].map(|x, i| c + x + y + i)[0]")?,
|
engine.eval::<INT>("let c = 40; let y = 1; [1].map(|x, i| c + x + y + i)[0]")?,
|
||||||
42
|
42
|
||||||
);
|
);
|
||||||
|
assert_eq!(
|
||||||
|
engine.eval::<INT>("let x = [1, 2, 3]; x.for_each(|i| this += i); x[2]")?,
|
||||||
|
5
|
||||||
|
);
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
engine
|
engine
|
||||||
@ -387,18 +395,31 @@ fn test_arrays_map_reduce() -> Result<(), Box<EvalAltResult>> {
|
|||||||
14
|
14
|
||||||
);
|
);
|
||||||
|
|
||||||
assert_eq!(
|
// assert_eq!(
|
||||||
engine.eval::<INT>(
|
// engine.eval::<INT>(
|
||||||
"
|
// "
|
||||||
let x = [1, 2, 3];
|
// let x = [1, 2, 3];
|
||||||
x.reduce(|sum, v, i| {
|
// x.reduce(|sum, v, i| {
|
||||||
if i == 0 { sum = 10 }
|
// if i == 0 { sum = 10 }
|
||||||
sum + v * v
|
// sum + v * v
|
||||||
})
|
// })
|
||||||
"
|
// "
|
||||||
)?,
|
// )?,
|
||||||
24
|
// 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!(
|
assert_eq!(
|
||||||
engine.eval::<INT>(
|
engine.eval::<INT>(
|
||||||
|
Loading…
Reference in New Issue
Block a user