diff --git a/doc/src/language/arrays.md b/doc/src/language/arrays.md
index d687e002..56706d86 100644
--- a/doc/src/language/arrays.md
+++ b/doc/src/language/arrays.md
@@ -48,8 +48,12 @@ The following methods (mostly defined in the [`BasicArrayPackage`][packages] but
| `clear` | _none_ | empties the array |
| `truncate` | target length | cuts off the array at exactly a specified length (discarding all subsequent elements) |
| `chop` | target length | cuts off the head of the array, leaving the tail at exactly a specified length |
+| `drain` | 1) [function pointer] to predicate (usually a [closure]),
2) _(optional)_ [function pointer] to function (usually a [closure]) that provides the initial value | removes all items (returning them) that return `true` when called with the predicate function:
1st parameter: array item,
2nd parameter: _(optional)_ offset index |
+| `drain` | 1) start position, beginning if < 0, end if > length,
2) number of items to remove, none if < 0 | removes a portion of the array, returning the removed items (not in original order) |
+| `retain` | 1) [function pointer] to predicate (usually a [closure]),
2) _(optional)_ [function pointer] to function (usually a [closure]) that provides the initial value | removes all items (returning them) that do not return `true` when called with the predicate function:
1st parameter: array item,
2nd parameter: _(optional)_ offset index |
+| `retain` | 1) start position, beginning if < 0, end if > length,
2) number of items to retain, none if < 0 | retains a portion of the array, removes all other items and returning them (not in original order) |
| `splice` | 1) start position, beginning if < 0, end if > length,
2) number of items to remove, none if < 0,
3) array to insert | replaces a portion of the array with another (not necessarily of the same length as the replaced portion) |
-| `filter` | [function pointer] to predicate (usually a [closure]) | constructs a new array with all items that returns `true` when called with the predicate function:
1st parameter: array item,
2nd parameter: _(optional)_ offset index |
+| `filter` | [function pointer] to predicate (usually a [closure]) | constructs a new array with all items that return `true` when called with the predicate function:
1st parameter: array item,
2nd parameter: _(optional)_ offset index |
| `map` | [function pointer] to conversion function (usually a [closure]) | constructs a new array with all items mapped to the result of applying the conversion function:
1st parameter: array item,
2nd parameter: _(optional)_ offset index |
| `reduce` | 1) [function pointer] to accumulator function (usually a [closure]),
2) _(optional)_ [function pointer] to function (usually a [closure]) that provides the initial value | reduces the array into a single value via the accumulator function:
1st parameter: accumulated value ([`()`] initially),
2nd parameter: array item,
3rd parameter: _(optional)_ offset index |
| `reduce_rev` | 1) [function pointer] to accumulator function (usually a [closure]),
2) _(optional)_ [function pointer] to function (usually a [closure]) that provides the initial value | reduces the array (in reverse order) into a single value via the accumulator function:
1st parameter: accumulated value ([`()`] initially),
2nd parameter: array item,
3rd parameter: _(optional)_ offset index |
@@ -199,4 +203,12 @@ a.splice(1, 1, [1, 3, 2]); // a == [42, 1, 3, 2, 99]
a.extract(1, 3); // returns [1, 3, 2]
a.sort(|x, y| x - y); // a == [1, 2, 3, 42, 99]
+
+a.drain(|v| v <= 1); // a == [2, 3, 42, 99]
+
+a.drain(|v, i| i >= 3); // a == [2, 3, 42]
+
+a.retain(|v| v > 10); // a == [42]
+
+a.retain(|v, i| i > 0); // a == []
```
diff --git a/src/packages/array_basic.rs b/src/packages/array_basic.rs
index 826a2196..5fa5b103 100644
--- a/src/packages/array_basic.rs
+++ b/src/packages/array_basic.rs
@@ -73,6 +73,8 @@ def_package!(crate:BasicArrayPackage:"Basic array utilities.", lib, {
lib.set_raw_fn("map", &[TypeId::of::(), TypeId::of::()], map);
lib.set_raw_fn("filter", &[TypeId::of::(), TypeId::of::()], filter);
+ lib.set_raw_fn("drain", &[TypeId::of::(), TypeId::of::()], drain);
+ lib.set_raw_fn("retain", &[TypeId::of::(), TypeId::of::()], retain);
lib.set_raw_fn("reduce", &[TypeId::of::(), TypeId::of::()], reduce);
lib.set_raw_fn("reduce", &[TypeId::of::(), TypeId::of::(), TypeId::of::()], reduce_with_initial);
lib.set_raw_fn("reduce_rev", &[TypeId::of::(), TypeId::of::()], reduce_rev);
@@ -191,6 +193,49 @@ mod array_functions {
list[start..].iter().cloned().collect()
}
+ #[rhai_fn(name = "drain")]
+ pub fn drain_range(list: &mut Array, start: INT, len: INT) -> Array {
+ let start = if start < 0 {
+ 0
+ } else if start as usize >= list.len() {
+ list.len() - 1
+ } else {
+ start as usize
+ };
+
+ let len = if len < 0 {
+ 0
+ } else if len as usize > list.len() - start {
+ list.len() - start
+ } else {
+ len as usize
+ };
+
+ list.drain(start..start + len - 1).collect()
+ }
+ #[rhai_fn(name = "retain")]
+ pub fn retain_range(list: &mut Array, start: INT, len: INT) -> Array {
+ let start = if start < 0 {
+ 0
+ } else if start as usize >= list.len() {
+ list.len() - 1
+ } else {
+ start as usize
+ };
+
+ let len = if len < 0 {
+ 0
+ } else if len as usize > list.len() - start {
+ list.len() - start
+ } else {
+ len as usize
+ };
+
+ let mut drained = list.drain(start + len..).collect::();
+ drained.extend(list.drain(..start));
+
+ drained
+ }
}
fn pad(
@@ -552,6 +597,86 @@ fn sort(
Ok(().into())
}
+fn drain(
+ engine: &Engine,
+ lib: &Module,
+ args: &mut [&mut Dynamic],
+) -> Result> {
+ let filter = args[1].read_lock::().unwrap().clone();
+ let mut list = args[0].write_lock::().unwrap();
+
+ let mut drained = Array::with_capacity(list.len());
+
+ let mut i = list.len();
+
+ while i > 0 {
+ i -= 1;
+
+ if filter
+ .call_dynamic(engine, lib, None, [list[i].clone()])
+ .or_else(|err| match *err {
+ EvalAltResult::ErrorFunctionNotFound(_, _) => {
+ filter.call_dynamic(engine, lib, None, [list[i].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)
+ {
+ drained.push(list.remove(i));
+ }
+ }
+
+ Ok(drained)
+}
+
+fn retain(
+ engine: &Engine,
+ lib: &Module,
+ args: &mut [&mut Dynamic],
+) -> Result> {
+ let filter = args[1].read_lock::().unwrap().clone();
+ let mut list = args[0].write_lock::().unwrap();
+
+ let mut drained = Array::with_capacity(list.len());
+
+ let mut i = list.len();
+
+ while i > 0 {
+ i -= 1;
+
+ if !filter
+ .call_dynamic(engine, lib, None, [list[i].clone()])
+ .or_else(|err| match *err {
+ EvalAltResult::ErrorFunctionNotFound(_, _) => {
+ filter.call_dynamic(engine, lib, None, [list[i].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)
+ {
+ drained.push(list.remove(i));
+ }
+ }
+
+ Ok(drained)
+}
+
gen_array_functions!(basic => INT, bool, char, ImmutableString, FnPtr, Array, Unit);
#[cfg(not(feature = "only_i32"))]