* Evaluate a reduce expression over a list. * Folds the list into a single value using an accumulator.
(
path: TraversalPath<any, any, any>,
reduce: {
accumulator: string;
variable: string;
expression: ConditionValue;
},
initialValue: any,
list: any[],
context?: QueryContext,
)
| 2068 | * Folds the list into a single value using an accumulator. |
| 2069 | */ |
| 2070 | function evaluateReduce( |
| 2071 | path: TraversalPath<any, any, any>, |
| 2072 | reduce: { |
| 2073 | accumulator: string; |
| 2074 | variable: string; |
| 2075 | expression: ConditionValue; |
| 2076 | }, |
| 2077 | initialValue: any, |
| 2078 | list: any[], |
| 2079 | context?: QueryContext, |
| 2080 | ): any { |
| 2081 | let accumulator = initialValue; |
| 2082 | |
| 2083 | for (const element of list) { |
| 2084 | // Create bindings for both the accumulator and the iteration variable |
| 2085 | // First, create a binding for the accumulator |
| 2086 | const accumulatorBinding = { |
| 2087 | label: "ComprehensionElement", |
| 2088 | value: accumulator, |
| 2089 | }; |
| 2090 | const pathWithAcc = new TraversalPath(path, accumulatorBinding as any, [reduce.accumulator]); |
| 2091 | |
| 2092 | // Then, create a binding for the iteration variable |
| 2093 | const iterationBinding = { |
| 2094 | label: "ComprehensionElement", |
| 2095 | value: element, |
| 2096 | }; |
| 2097 | const boundPath = new TraversalPath(pathWithAcc, iterationBinding as any, [reduce.variable]); |
| 2098 | |
| 2099 | // Evaluate the expression to get the new accumulator value |
| 2100 | accumulator = resolveConditionValue(boundPath, reduce.expression, context); |
| 2101 | } |
| 2102 | |
| 2103 | return accumulator; |
| 2104 | } |
| 2105 | |
| 2106 | /** |
| 2107 | * Compare two values using the given operator. |
no test coverage detected