1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
use rhai::plugin::*;
#[export_module]
pub mod set_functions {
use rhai::{Array, Dynamic, EvalAltResult};
/// Returns the set union of two arrays.
/// ```typescript
/// let set1 = [7, 1, 7, 7, 4];
/// let set2 = [7, 0, 4, 4, 0];
/// let u = union(set1, set2);
/// assert_eq(u, [0, 1, 4, 7]);
/// ```
#[rhai_fn(name = "union", return_raw)]
pub fn union(arr1: Array, arr2: Array) -> Result<Array, Box<EvalAltResult>> {
let mut x = arr1.clone();
let y = arr2.clone();
x.extend(y);
crate::misc_functions::unique(&mut x)
}
/// Performs set intersection of two arrays
/// ```typescript
/// let set1 = [7, 1, 7, 7, 4];
/// let set2 = [7, 0, 4, 4, 0];
/// let x = intersect(set1, set2);
/// assert_eq(x, [4, 7]);
/// ```
#[rhai_fn(name = "intersect", return_raw)]
pub fn intersect(arr1: Array, arr2: Array) -> Result<Array, Box<EvalAltResult>> {
let array2 = arr2
.into_iter()
.map(|x| format!("{:?}", x).to_string())
.collect::<Vec<String>>();
let mut new_arr = vec![];
for el in arr1 {
if array2.contains(&format!("{:?}", el).to_string()) {
new_arr.push(el);
}
}
Ok(crate::misc_functions::unique(&mut new_arr).unwrap())
}
}