Add array API with closure variation that binds to this.
This commit is contained in:
@@ -647,6 +647,10 @@ pub mod array_functions {
|
||||
/// 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.
|
||||
///
|
||||
/// # No Function Parameter
|
||||
///
|
||||
/// Array element (mutable) is bound to `this`.
|
||||
///
|
||||
/// # Function Parameters
|
||||
///
|
||||
/// * `element`: copy of array element
|
||||
@@ -666,17 +670,17 @@ pub mod array_functions {
|
||||
/// print(y); // prints "[0, 2, 6, 12, 20]"
|
||||
/// ```
|
||||
#[rhai_fn(return_raw)]
|
||||
pub fn map(ctx: NativeCallContext, array: Array, map: FnPtr) -> RhaiResultOf<Array> {
|
||||
pub fn map(ctx: NativeCallContext, array: &mut Array, map: FnPtr) -> RhaiResultOf<Array> {
|
||||
if array.is_empty() {
|
||||
return Ok(array);
|
||||
return Ok(Array::new());
|
||||
}
|
||||
|
||||
let mut ar = Array::with_capacity(array.len());
|
||||
|
||||
for (i, item) in array.into_iter().enumerate() {
|
||||
for (i, item) in array.iter_mut().enumerate() {
|
||||
let ex = [(i as INT).into()];
|
||||
|
||||
ar.push(map.call_raw_with_extra_args("map", &ctx, None, [item], ex)?);
|
||||
ar.push(map.call_raw_with_extra_args("map", &ctx, Some(item), [], ex)?);
|
||||
}
|
||||
|
||||
Ok(ar)
|
||||
@@ -685,6 +689,10 @@ pub mod array_functions {
|
||||
/// Iterate through all the elements in the array, applying a `filter` function to each element
|
||||
/// in turn, and return a copy of all elements (in order) that return `true` as a new array.
|
||||
///
|
||||
/// # No Function Parameter
|
||||
///
|
||||
/// Array element (mutable) is bound to `this`.
|
||||
///
|
||||
/// # Function Parameters
|
||||
///
|
||||
/// * `element`: copy of array element
|
||||
@@ -704,22 +712,22 @@ pub mod array_functions {
|
||||
/// print(y); // prints "[12, 20]"
|
||||
/// ```
|
||||
#[rhai_fn(return_raw)]
|
||||
pub fn filter(ctx: NativeCallContext, array: Array, filter: FnPtr) -> RhaiResultOf<Array> {
|
||||
pub fn filter(ctx: NativeCallContext, array: &mut Array, filter: FnPtr) -> RhaiResultOf<Array> {
|
||||
if array.is_empty() {
|
||||
return Ok(array);
|
||||
return Ok(Array::new());
|
||||
}
|
||||
|
||||
let mut ar = Array::new();
|
||||
|
||||
for (i, item) in array.into_iter().enumerate() {
|
||||
for (i, item) in array.iter_mut().enumerate() {
|
||||
let ex = [(i as INT).into()];
|
||||
|
||||
if filter
|
||||
.call_raw_with_extra_args("filter", &ctx, None, [item.clone()], ex)?
|
||||
.call_raw_with_extra_args("filter", &ctx, Some(item), [], ex)?
|
||||
.as_bool()
|
||||
.unwrap_or(false)
|
||||
{
|
||||
ar.push(item);
|
||||
ar.push(item.clone());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -871,6 +879,10 @@ pub mod array_functions {
|
||||
/// in turn, and return the index of the first element that returns `true`.
|
||||
/// If no element returns `true`, `-1` is returned.
|
||||
///
|
||||
/// # No Function Parameter
|
||||
///
|
||||
/// Array element (mutable) is bound to `this`.
|
||||
///
|
||||
/// # Function Parameters
|
||||
///
|
||||
/// * `element`: copy of array element
|
||||
@@ -907,6 +919,10 @@ pub mod array_functions {
|
||||
/// * If `start` < -length of array, position counts from the beginning of the array.
|
||||
/// * If `start` ≥ length of array, `-1` is returned.
|
||||
///
|
||||
/// # No Function Parameter
|
||||
///
|
||||
/// Array element (mutable) is bound to `this`.
|
||||
///
|
||||
/// # Function Parameters
|
||||
///
|
||||
/// * `element`: copy of array element
|
||||
@@ -942,11 +958,11 @@ pub mod array_functions {
|
||||
|
||||
let (start, ..) = calc_offset_len(array.len(), start, 0);
|
||||
|
||||
for (i, item) in array.iter().enumerate().skip(start) {
|
||||
for (i, item) in array.iter_mut().enumerate().skip(start) {
|
||||
let ex = [(i as INT).into()];
|
||||
|
||||
if filter
|
||||
.call_raw_with_extra_args("index_of", &ctx, None, [item.clone()], ex)?
|
||||
.call_raw_with_extra_args("index_of", &ctx, Some(item), [], ex)?
|
||||
.as_bool()
|
||||
.unwrap_or(false)
|
||||
{
|
||||
@@ -960,6 +976,10 @@ pub mod array_functions {
|
||||
/// in turn, and return a copy of the first element that returns `true`. If no element returns
|
||||
/// `true`, `()` is returned.
|
||||
///
|
||||
/// # No Function Parameter
|
||||
///
|
||||
/// Array element (mutable) is bound to `this`.
|
||||
///
|
||||
/// # Function Parameters
|
||||
///
|
||||
/// * `element`: copy of array element
|
||||
@@ -972,7 +992,7 @@ pub mod array_functions {
|
||||
///
|
||||
/// print(x.find(|v| v > 3)); // prints 5: 5 > 3
|
||||
///
|
||||
/// x.find(|v| v > 13) ?? print("not found"); // prints "not found": nothing is > 13
|
||||
/// print(x.find(|v| v > 13) ?? "not found"); // prints "not found": nothing is > 13
|
||||
///
|
||||
/// print(x.find(|v, i| v * i > 13)); // prints 5: 3 * 5 > 13
|
||||
/// ```
|
||||
@@ -988,6 +1008,10 @@ pub mod array_functions {
|
||||
/// * If `start` < -length of array, position counts from the beginning of the array.
|
||||
/// * If `start` ≥ length of array, `-1` is returned.
|
||||
///
|
||||
/// # No Function Parameter
|
||||
///
|
||||
/// Array element (mutable) is bound to `this`.
|
||||
///
|
||||
/// # Function Parameters
|
||||
///
|
||||
/// * `element`: copy of array element
|
||||
@@ -1000,9 +1024,9 @@ pub mod array_functions {
|
||||
///
|
||||
/// print(x.find(|v| v > 1, 2)); // prints 3: 3 > 1
|
||||
///
|
||||
/// x.find(|v| v < 2, 3) ?? print("not found"); // prints "not found": nothing < 2 past index 3
|
||||
/// print(x.find(|v| v < 2, 3) ?? "not found"); // prints "not found": nothing < 2 past index 3
|
||||
///
|
||||
/// x.find(|v| v > 1, 8) ?? print("not found"); // prints "not found": nothing found past end of array
|
||||
/// print(x.find(|v| v > 1, 8) ?? "not found"); // prints "not found": nothing found past end of array
|
||||
///
|
||||
/// print(x.find(|v| v > 1, -3)); // prints 5: -3 = start from index 4
|
||||
///
|
||||
@@ -1028,6 +1052,10 @@ pub mod array_functions {
|
||||
/// Iterate through all the elements in the array, applying a `mapper` function to each element
|
||||
/// in turn, and return the first result that is not `()`. Otherwise, `()` is returned.
|
||||
///
|
||||
/// # No Function Parameter
|
||||
///
|
||||
/// Array element (mutable) is bound to `this`.
|
||||
///
|
||||
/// # Function Parameters
|
||||
///
|
||||
/// * `element`: copy of array element
|
||||
@@ -1040,7 +1068,9 @@ pub mod array_functions {
|
||||
///
|
||||
/// print(x.find_map(|v| v.alice)); // prints 1
|
||||
///
|
||||
/// x.find_map(|v| v.dave) ?? print("not found"); // prints "not found"
|
||||
/// print(x.find_map(|v| v.dave) ?? "not found"); // prints "not found"
|
||||
///
|
||||
/// print(x.find_map(|| this.dave) ?? "not found"); // prints "not found"
|
||||
/// ```
|
||||
#[rhai_fn(return_raw, pure)]
|
||||
pub fn find_map(ctx: NativeCallContext, array: &mut Array, filter: FnPtr) -> RhaiResult {
|
||||
@@ -1054,6 +1084,10 @@ pub mod array_functions {
|
||||
/// * If `start` < -length of array, position counts from the beginning of the array.
|
||||
/// * If `start` ≥ length of array, `-1` is returned.
|
||||
///
|
||||
/// # No Function Parameter
|
||||
///
|
||||
/// Array element (mutable) is bound to `this`.
|
||||
///
|
||||
/// # Function Parameters
|
||||
///
|
||||
/// * `element`: copy of array element
|
||||
@@ -1066,13 +1100,17 @@ pub mod array_functions {
|
||||
///
|
||||
/// print(x.find_map(|v| v.alice, 2)); // prints 0
|
||||
///
|
||||
/// x.find_map(|v| v.bob, 4) ?? print("not found"); // prints "not found"
|
||||
/// print(x.find_map(|v| v.bob, 4) ?? "not found"); // prints "not found"
|
||||
///
|
||||
/// x.find_map(|v| v.alice, 8) ?? print("not found"); // prints "not found"
|
||||
/// print(x.find_map(|v| v.alice, 8) ?? "not found"); // prints "not found"
|
||||
///
|
||||
/// print(x.find_map(|| this.alice, 8) ?? "not found"); // prints "not found"
|
||||
///
|
||||
/// print(x.find_map(|v| v.bob, -4)); // prints 3: -4 = start from index 2
|
||||
///
|
||||
/// print(x.find_map(|v| v.alice, -99)); // prints 1: -99 = start from beginning
|
||||
///
|
||||
/// print(x.find_map(|| this.alice, -99)); // prints 1: -99 = start from beginning
|
||||
/// ```
|
||||
#[rhai_fn(name = "find_map", return_raw, pure)]
|
||||
pub fn find_map_starting_from(
|
||||
@@ -1087,11 +1125,10 @@ pub mod array_functions {
|
||||
|
||||
let (start, ..) = calc_offset_len(array.len(), start, 0);
|
||||
|
||||
for (i, item) in array.iter().enumerate().skip(start) {
|
||||
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, None, [item.clone()], ex)?;
|
||||
let value = filter.call_raw_with_extra_args("find_map", &ctx, Some(item), [], ex)?;
|
||||
|
||||
if !value.is_unit() {
|
||||
return Ok(value);
|
||||
@@ -1102,6 +1139,10 @@ pub mod array_functions {
|
||||
}
|
||||
/// Return `true` if any element in the array that returns `true` when applied the `filter` function.
|
||||
///
|
||||
/// # No Function Parameter
|
||||
///
|
||||
/// Array element (mutable) is bound to `this`.
|
||||
///
|
||||
/// # Function Parameters
|
||||
///
|
||||
/// * `element`: copy of array element
|
||||
@@ -1124,11 +1165,11 @@ pub mod array_functions {
|
||||
return Ok(false);
|
||||
}
|
||||
|
||||
for (i, item) in array.iter().enumerate() {
|
||||
for (i, item) in array.iter_mut().enumerate() {
|
||||
let ex = [(i as INT).into()];
|
||||
|
||||
if filter
|
||||
.call_raw_with_extra_args("some", &ctx, None, [item.clone()], ex)?
|
||||
.call_raw_with_extra_args("some", &ctx, Some(item), [], ex)?
|
||||
.as_bool()
|
||||
.unwrap_or(false)
|
||||
{
|
||||
@@ -1140,6 +1181,10 @@ pub mod array_functions {
|
||||
}
|
||||
/// Return `true` if all elements in the array return `true` when applied the `filter` function.
|
||||
///
|
||||
/// # No Function Parameter
|
||||
///
|
||||
/// Array element (mutable) is bound to `this`.
|
||||
///
|
||||
/// # Function Parameters
|
||||
///
|
||||
/// * `element`: copy of array element
|
||||
@@ -1162,11 +1207,11 @@ pub mod array_functions {
|
||||
return Ok(true);
|
||||
}
|
||||
|
||||
for (i, item) in array.iter().enumerate() {
|
||||
for (i, item) in array.iter_mut().enumerate() {
|
||||
let ex = [(i as INT).into()];
|
||||
|
||||
if !filter
|
||||
.call_raw_with_extra_args("all", &ctx, None, [item.clone()], ex)?
|
||||
.call_raw_with_extra_args("all", &ctx, Some(item), [], ex)?
|
||||
.as_bool()
|
||||
.unwrap_or(false)
|
||||
{
|
||||
@@ -1516,6 +1561,10 @@ pub mod array_functions {
|
||||
/// Remove all elements in the array that returns `true` when applied the `filter` function and
|
||||
/// return them as a new array.
|
||||
///
|
||||
/// # No Function Parameter
|
||||
///
|
||||
/// Array element (mutable) is bound to `this`.
|
||||
///
|
||||
/// # Function Parameters
|
||||
///
|
||||
/// * `element`: copy of array element
|
||||
@@ -1553,7 +1602,7 @@ pub mod array_functions {
|
||||
let ex = [(i as INT).into()];
|
||||
|
||||
if filter
|
||||
.call_raw_with_extra_args("drain", &ctx, None, [array[x].clone()], ex)?
|
||||
.call_raw_with_extra_args("drain", &ctx, Some(&mut array[x]), [], ex)?
|
||||
.as_bool()
|
||||
.unwrap_or(false)
|
||||
{
|
||||
@@ -1659,6 +1708,10 @@ pub mod array_functions {
|
||||
/// Remove all elements in the array that do not return `true` when applied the `filter`
|
||||
/// function and return them as a new array.
|
||||
///
|
||||
/// # No Function Parameter
|
||||
///
|
||||
/// Array element (mutable) is bound to `this`.
|
||||
///
|
||||
/// # Function Parameters
|
||||
///
|
||||
/// * `element`: copy of array element
|
||||
@@ -1696,7 +1749,7 @@ pub mod array_functions {
|
||||
let ex = [(i as INT).into()];
|
||||
|
||||
if filter
|
||||
.call_raw_with_extra_args("retain", &ctx, None, [array[x].clone()], ex)?
|
||||
.call_raw_with_extra_args("retain", &ctx, Some(&mut array[x]), [], ex)?
|
||||
.as_bool()
|
||||
.unwrap_or(false)
|
||||
{
|
||||
|
Reference in New Issue
Block a user