MCPcopy Create free account
hub / github.com/PowerShell/DSC / apply_lambda_to_array

Function apply_lambda_to_array

lib/dsc-lib/src/functions/lambda_helpers.rs:67–102  ·  view source on GitHub ↗

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,
)

Source from the content-addressed store, hash-verified

65///
66/// A vector of values produced by the `apply` closure.
67pub fn apply_lambda_to_array<F>(
68 array: &[Value],
69 lambda: &Lambda,
70 context: &Context,
71 mut apply: F,
72) -> Result<Vec<Value>, DscError>
73where
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}

Callers 2

invokeMethod · 0.85
invokeMethod · 0.85

Calls 2

newFunction · 0.50
invokeMethod · 0.45

Tested by

no test coverage detected