ReduceList¶
Description¶
The ReduceList object is a PropertyModifier object which reduces a source list to a single value using an arbitrary expression. The expression is evaluated for each element in the source list and the final result written to the target property. ReduceList manages strong bindings between elements in the source list, the expression and the reduced value, i.e. whenever either an element in the source list or other variable parts of the evaluated expression change the target property is being updated instantly.
ReduceList primarily is designed to use with DataObject lists. ReduceList replaces traditional for
loops in a declarative manner which often allows simplifying the code and thus reduce its complexity.
› Inherits: | PropertyModifier |
---|
Properties¶
accumulatorInitValue¶
This property holds the initial value for the accumulator. It should be set even for default values (such as false
, 0
or ""
) so that the type for accumulator can be determined properly.
› Type: | Variant |
---|---|
› Signal: | accumulatorInitValueChanged() |
› Attributes: | Writable |
error¶
This property holds the most recently occurred error or ReduceList.NoError if no error occurred. If the same error occurs multiple times this property does not change. Use the errorOccurred() signal to detect multiple occurrences of the same error.
› Type: | Error |
---|---|
› Signal: | errorChanged() |
› Attributes: | Readonly |
errorString¶
This property holds the current human readable error string corresponding to the current value in the error property. It may include additional information such as failure reasons or locations.
› Type: | String |
---|---|
› Signal: | errorStringChanged() |
› Attributes: | Readonly |
eval¶
This property holds an expression which is evaluated for each element in the source list. The respective source list element is available in a local item
property inside the expression. The result of the expression is written to a local accumulator property which can be read again in the next iteration. After the last element has been processed the accumulator value is written to the target property. The expression is reevaluated whenever the source list element or other parts of the expression change. The target property will therefore always be up-to-date automatically.
› Type: | <QML expression> |
---|---|
› Signal: | evalChanged() |
› Attributes: | Writable |
Signals¶
Enumerations¶
Error¶
This enumeration describes all errors which can occur in ReduceList objects. The most recently occurred error is stored in the error property.
Name | Value | Description |
---|---|---|
ReduceList.NoError |
0 |
No error occurred or was detected. |
ReduceList.InvalidSource |
1 |
Operation not supported for empty or non-list source. |
ReduceList.EvalExpressionError |
2 |
Error while evaluating expression:: <Unknown File>: . |
ReduceList.TargetWriteError |
3 |
The result value could not be written to the target property, likely due to type incompatibilities. |
Example¶
import InCore.Foundation 2.5
Application {
List {
id: simpleValueList
items: [ 1, 2, 3 ]
}
property int listSum: 0;
ReduceList on listSum {
accumulatorInitValue: 0
source: simpleValueList
eval: accumulator + item
}
onListSumChanged: console.log("List value sum:", listSum)
MeasurementGroup {
id: temperatures
Measurement { id: temp1; data: 3 }
Measurement { id: temp2; data: 4 }
Measurement { id: temp3; data: 5 }
}
property bool dangerOfFrost: false
ReduceList on dangerOfFrost {
accumulatorInitValue: false
source: temperatures.objects
eval: accumulator || item.data < 3
}
onDangerOfFrostChanged: console.log("Danger of frost:", dangerOfFrost)
onCompleted: {
console.log("Updating value list item")
simpleValueList.setItem( 2, 123 );
console.log("Decreasing temperature")
temp1.data--;
console.log("Increasing temperature")
temp1.data++;
}
}