Applies a lambda to each element of an array, yielding transformed values. This is the core iteration logic shared by `map()`, `filter()`, and similar lambda-consuming functions. # Arguments `array` - The input array to iterate over `lambda` - The lambda to apply to each element `context` - The base context (will be cloned for each iteration) `mut apply` - A closure that receives the lambda res
(
array: &[Value],
lambda: &Lambda,
context: &Context,
mut apply: F,
)
| 65 | /// |
| 66 | /// A vector of values produced by the `apply` closure. |
| 67 | pub fn apply_lambda_to_array<F>( |
| 68 | array: &[Value], |
| 69 | lambda: &Lambda, |
| 70 | context: &Context, |
| 71 | mut apply: F, |
| 72 | ) -> Result<Vec<Value>, DscError> |
| 73 | where |
| 74 | F: FnMut(Value, &Value) -> Result<Option<Value>, DscError>, |
| 75 | { |
| 76 | let dispatcher = FunctionDispatcher::new(); |
| 77 | let mut results = Vec::new(); |
| 78 | |
| 79 | for (index, element) in array.iter().enumerate() { |
| 80 | let mut lambda_context = context.clone(); |
| 81 | |
| 82 | lambda_context.lambda_variables.insert( |
| 83 | lambda.parameters[0].clone(), |
| 84 | element.clone() |
| 85 | ); |
| 86 | |
| 87 | if lambda.parameters.len() == 2 { |
| 88 | lambda_context.lambda_variables.insert( |
| 89 | lambda.parameters[1].clone(), |
| 90 | Value::Number(serde_json::Number::from(index)) |
| 91 | ); |
| 92 | } |
| 93 | |
| 94 | let result = lambda.body.invoke(&dispatcher, &lambda_context)?; |
| 95 | |
| 96 | if let Some(value) = apply(result, element)? { |
| 97 | results.push(value); |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | Ok(results) |
| 102 | } |