Reduce operation #48
Replies: 7 comments 29 replies
-
My post in the other discussion goes into more detail but I think we should consider implementing this as syntax. As far as I can tell |
Beta Was this translation helpful? Give feedback.
-
To summarize a few different discussions I think the best standing proposal is for the reduce operation to be:
Like the projection operation, the expression following the reducing projection is applied to every value of an array or map. The The The resulting value of the reducing-projection is the accumulated value (or the value of the last iteration). examples:
|
Beta Was this translation helpful? Give feedback.
-
Based on this comment I think the reduce operation is not a projection. In that it produces a single value and does not expect downstream expressions to apply on multiple items from the result of the operation. |
Beta Was this translation helpful? Give feedback.
-
For consistency reason, since we have |
Beta Was this translation helpful? Give feedback.
-
For the record, here is an alternate proposal for a
The The first property of the In a reduce operation, the accumulated value starts as the seed. So the name of the first property in the Thus,
ProsReuses concepts already introduced by the ConsNone 🤣 |
Beta Was this translation helpful? Give feedback.
-
Some of us are keen to try and support a reduce operation through pure syntax instead of relying on a function.
Here Instead of thinking of the reduce being split into a left projection that projects its values to a right reduce operation, let’s invert the process and have a
Here the bracket part is not a expression =/ reduce-expression
sub-expression =/ expression "." reduce-expression
reduce-expression = "[%" [ expression ] "," expression-type "]" Popular functions, like
|
Beta Was this translation helpful? Give feedback.
-
In light of a newly proposed
Maybe a new
Where
And it would call the |
Beta Was this translation helpful? Give feedback.
-
We have map to apply the same operation resulting in the same size output, but we don't have a reduce operation that allows aggregating an arbitrary dataset.
The reduce operation is a bit complicated as the expression that is being evaluated for each input must accept two inputs, the aggregate so far and the current input. There is currently no way in JMESPath to refer to more than one item in context
@
. Thelet()
function is slated to resolve that for most use cases, but it can not be used recursively.To contrast
map(&expression, array)
, expression accesses the current item via@
when being evaluated.We could do some trickery with an implied
let()
around each evaluation of the expression with a reserved identifier for the aggregate, but that is a bit cludgey.We could equally introduce an additional symbol rather than an identifier, but that is not much better and it adds single use grammar for a function.
The expression could have a wrapped context where
@
is an object like{"acc":.., "cur":..}
and the expression must explicitly refer to each, but that is not much better than the first suggestion, putting the current item behind an additional identifier as well. At least with this there is no possibility of the aggregate identifier colliding with an identifier in the current item.I am interested to hear alternatives or if any of these suggestions are preferred.
Beta Was this translation helpful? Give feedback.
All reactions