From 7bca0f1407daf7104f5fa403c28ab6ef716008fd Mon Sep 17 00:00:00 2001 From: Tim Fennis Date: Thu, 17 Apr 2025 15:08:00 +0200 Subject: [PATCH] Added a locate variant that locates index of elements by value --- ndc_lib/src/stdlib/sequence.rs | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/ndc_lib/src/stdlib/sequence.rs b/ndc_lib/src/stdlib/sequence.rs index 5162ceb1..39e37cd1 100644 --- a/ndc_lib/src/stdlib/sequence.rs +++ b/ndc_lib/src/stdlib/sequence.rs @@ -340,7 +340,20 @@ mod inner { } } - Err(anyhow!("find did not find anything").into()) + Err(anyhow!("locate did not find anything").into()) + } + + /// Returns the first index of the element or produces an error + #[function(name = "locate")] + pub fn locate_element(seq: &mut Sequence, element: &Value) -> EvaluationResult { + let iterator = mut_seq_to_iterator(seq); + for (idx, el) in iterator.enumerate() { + if &el == element { + return Ok(Value::from(idx)); + } + } + + Err(anyhow!("locate did not find anything").into()) } /// Returns `true` if the `predicate` is true for none of the elements in `seq`.