Add drain and retain for arrays.
This commit is contained in:
parent
aa6d00f253
commit
58c820785b
@ -48,8 +48,12 @@ The following methods (mostly defined in the [`BasicArrayPackage`][packages] but
|
|||||||
| `clear` | _none_ | empties the array |
|
| `clear` | _none_ | empties the array |
|
||||||
| `truncate` | target length | cuts off the array at exactly a specified length (discarding all subsequent elements) |
|
| `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 |
|
| `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]),<br/>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:<br/>1st parameter: array item,<br/>2nd parameter: _(optional)_ offset index |
|
||||||
|
| `drain` | 1) start position, beginning if < 0, end if > length,<br/>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]),<br/>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:<br/>1st parameter: array item,<br/>2nd parameter: _(optional)_ offset index |
|
||||||
|
| `retain` | 1) start position, beginning if < 0, end if > length,<br/>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,<br/>2) number of items to remove, none if < 0,<br/>3) array to insert | replaces a portion of the array with another (not necessarily of the same length as the replaced portion) |
|
| `splice` | 1) start position, beginning if < 0, end if > length,<br/>2) number of items to remove, none if < 0,<br/>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:<br/>1st parameter: array item,<br/>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:<br/>1st parameter: array item,<br/>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:<br/>1st parameter: array item,<br/>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:<br/>1st parameter: array item,<br/>2nd parameter: _(optional)_ offset index |
|
||||||
| `reduce` | 1) [function pointer] to accumulator function (usually a [closure]),<br/>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:<br/>1st parameter: accumulated value ([`()`] initially),<br/>2nd parameter: array item,<br/>3rd parameter: _(optional)_ offset index |
|
| `reduce` | 1) [function pointer] to accumulator function (usually a [closure]),<br/>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:<br/>1st parameter: accumulated value ([`()`] initially),<br/>2nd parameter: array item,<br/>3rd parameter: _(optional)_ offset index |
|
||||||
| `reduce_rev` | 1) [function pointer] to accumulator function (usually a [closure]),<br/>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:<br/>1st parameter: accumulated value ([`()`] initially),<br/>2nd parameter: array item,<br/>3rd parameter: _(optional)_ offset index |
|
| `reduce_rev` | 1) [function pointer] to accumulator function (usually a [closure]),<br/>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:<br/>1st parameter: accumulated value ([`()`] initially),<br/>2nd parameter: array item,<br/>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.extract(1, 3); // returns [1, 3, 2]
|
||||||
|
|
||||||
a.sort(|x, y| x - y); // a == [1, 2, 3, 42, 99]
|
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 == []
|
||||||
```
|
```
|
||||||
|
@ -73,6 +73,8 @@ def_package!(crate:BasicArrayPackage:"Basic array utilities.", lib, {
|
|||||||
|
|
||||||
lib.set_raw_fn("map", &[TypeId::of::<Array>(), TypeId::of::<FnPtr>()], 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("filter", &[TypeId::of::<Array>(), TypeId::of::<FnPtr>()], filter);
|
||||||
|
lib.set_raw_fn("drain", &[TypeId::of::<Array>(), TypeId::of::<FnPtr>()], drain);
|
||||||
|
lib.set_raw_fn("retain", &[TypeId::of::<Array>(), TypeId::of::<FnPtr>()], retain);
|
||||||
lib.set_raw_fn("reduce", &[TypeId::of::<Array>(), TypeId::of::<FnPtr>()], reduce);
|
lib.set_raw_fn("reduce", &[TypeId::of::<Array>(), TypeId::of::<FnPtr>()], reduce);
|
||||||
lib.set_raw_fn("reduce", &[TypeId::of::<Array>(), TypeId::of::<FnPtr>(), TypeId::of::<FnPtr>()], reduce_with_initial);
|
lib.set_raw_fn("reduce", &[TypeId::of::<Array>(), TypeId::of::<FnPtr>(), TypeId::of::<FnPtr>()], reduce_with_initial);
|
||||||
lib.set_raw_fn("reduce_rev", &[TypeId::of::<Array>(), TypeId::of::<FnPtr>()], reduce_rev);
|
lib.set_raw_fn("reduce_rev", &[TypeId::of::<Array>(), TypeId::of::<FnPtr>()], reduce_rev);
|
||||||
@ -191,6 +193,49 @@ mod array_functions {
|
|||||||
|
|
||||||
list[start..].iter().cloned().collect()
|
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::<Array>();
|
||||||
|
drained.extend(list.drain(..start));
|
||||||
|
|
||||||
|
drained
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn pad<T: Variant + Clone>(
|
fn pad<T: Variant + Clone>(
|
||||||
@ -552,6 +597,86 @@ fn sort(
|
|||||||
Ok(().into())
|
Ok(().into())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn drain(
|
||||||
|
engine: &Engine,
|
||||||
|
lib: &Module,
|
||||||
|
args: &mut [&mut Dynamic],
|
||||||
|
) -> Result<Array, Box<EvalAltResult>> {
|
||||||
|
let filter = args[1].read_lock::<FnPtr>().unwrap().clone();
|
||||||
|
let mut list = args[0].write_lock::<Array>().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<Array, Box<EvalAltResult>> {
|
||||||
|
let filter = args[1].read_lock::<FnPtr>().unwrap().clone();
|
||||||
|
let mut list = args[0].write_lock::<Array>().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);
|
gen_array_functions!(basic => INT, bool, char, ImmutableString, FnPtr, Array, Unit);
|
||||||
|
|
||||||
#[cfg(not(feature = "only_i32"))]
|
#[cfg(not(feature = "only_i32"))]
|
||||||
|
Loading…
Reference in New Issue
Block a user