Function reduce() iterates a designated calculation against each elements in a list one after another. Developers need to define the calculation via an expression, firstly execute the calculation based on an initial value and the 1st element in the list, then re-execute the calculation based on the calculation result and the 2nd element in the list, ... until all elements in the list are traversed.
NOTEThe iteration pattern of this function is similar to the 'for' in most programming languages, just that the index of each iteration is not exposed to developers.
Syntax:
Syntaxreduce(<result> = <initial_value>, <element> in <list> | <expression>)
Example: Calculate the sum of all numbers in [1,2,3]
UQLwith [1,2,3] as list return reduce(sum = 0, element in list | sum + element) as mySum
Result6
Sample graph: (to be used for the following examples)

Example: Calculate the share of each UBO of F001
UQLn({_id == "F001"}).le()[:5].n({@human} as UBO) as p with pedges(p) as edgeList call{ with edgeList uncollect edgeList as edges with collect(edges.portion) as portionList return reduce(init = 1, element in portionList | init * element) as share } group by UBO return table(UBO._id, sum(share))
Result| UBO._id | sum(share) | |---------|------------| | H001 | 0.58 | | H002 | 0.42 |