Add dedup to array.
This commit is contained in:
parent
9c16d90de9
commit
1c921f3784
@ -14,6 +14,7 @@ Enhancements
|
|||||||
|
|
||||||
* Array methods now avoid cloning as much as possible (although most predicates will involve cloning anyway if passed a closure).
|
* Array methods now avoid cloning as much as possible (although most predicates will involve cloning anyway if passed a closure).
|
||||||
* Array methods that take function pointers (e.g. closures) now optionally take the function name as a string.
|
* Array methods that take function pointers (e.g. closures) now optionally take the function name as a string.
|
||||||
|
* Array adds the `dedup` method.
|
||||||
* Inlining is disabled for error-path functions because errors are exceptional and scripts usually fail completely when an error is encountered.
|
* Inlining is disabled for error-path functions because errors are exceptional and scripts usually fail completely when an error is encountered.
|
||||||
|
|
||||||
Deprecated API's
|
Deprecated API's
|
||||||
|
@ -637,6 +637,43 @@ mod array_functions {
|
|||||||
|
|
||||||
Ok(true)
|
Ok(true)
|
||||||
}
|
}
|
||||||
|
#[rhai_fn(return_raw)]
|
||||||
|
pub fn dedup(ctx: NativeCallContext, array: &mut Array) -> Result<(), Box<EvalAltResult>> {
|
||||||
|
dedup_with_fn_name(ctx, array, OP_EQUALS)
|
||||||
|
}
|
||||||
|
#[rhai_fn(name = "dedup", return_raw)]
|
||||||
|
pub fn dedup_by_comparer(
|
||||||
|
ctx: NativeCallContext,
|
||||||
|
array: &mut Array,
|
||||||
|
comparer: FnPtr,
|
||||||
|
) -> Result<(), Box<EvalAltResult>> {
|
||||||
|
if array.is_empty() {
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
|
||||||
|
dedup_with_fn_name(ctx, array, comparer.fn_name())
|
||||||
|
}
|
||||||
|
#[rhai_fn(name = "dedup", return_raw)]
|
||||||
|
fn dedup_with_fn_name(
|
||||||
|
ctx: NativeCallContext,
|
||||||
|
array: &mut Array,
|
||||||
|
comparer: &str,
|
||||||
|
) -> Result<(), Box<EvalAltResult>> {
|
||||||
|
if array.is_empty() {
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
|
||||||
|
array.dedup_by(|x, y| {
|
||||||
|
let mut args = [x, &mut y.clone()];
|
||||||
|
|
||||||
|
ctx.call_fn_raw(comparer, true, false, &mut args)
|
||||||
|
.unwrap_or_else(|_| Dynamic::FALSE)
|
||||||
|
.as_bool()
|
||||||
|
.unwrap_or(false)
|
||||||
|
});
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
#[rhai_fn(name = "reduce", return_raw, pure)]
|
#[rhai_fn(name = "reduce", return_raw, pure)]
|
||||||
pub fn reduce_with_fn_name(
|
pub fn reduce_with_fn_name(
|
||||||
ctx: NativeCallContext,
|
ctx: NativeCallContext,
|
||||||
|
Loading…
Reference in New Issue
Block a user