Add array find and find_map methods

This commit is contained in:
Fabrizio Sestito
2022-12-20 13:28:24 +01:00
parent 04a963ab41
commit 06b40723e4
2 changed files with 192 additions and 0 deletions

View File

@@ -423,6 +423,56 @@ fn test_arrays_map_reduce() -> Result<(), Box<EvalAltResult>> {
"
)?);
assert_eq!(
engine.eval::<INT>(
"
let x = [1, 2, 3];
x.find(|v| v > 2)
"
)?,
3
);
assert_eq!(
engine.eval::<INT>(
"
let x = [1, 2, 3];
x.find(|v, i| v * i == 6)
"
)?,
3
);
assert_eq!(
engine.eval::<()>(
"
let x = [1, 2, 3, 2, 1];
x.find(|v| v > 4)
"
)?,
()
);
assert_eq!(
engine.eval::<INT>(
"
let x = [#{alice: 1}, #{bob: 2}, #{clara: 3}];
x.find_map(|v| v.bob)
"
)?,
2
);
assert_eq!(
engine.eval::<()>(
"
let x = [#{alice: 1}, #{bob: 2}, #{clara: 3}];
x.find_map(|v| v.dave)
"
)?,
()
);
Ok(())
}