Implement reduceTransitions to allow efficient accumulation over transitions #78
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The folding operation
reduce
is a principle iteration method for sequences of any kind. It takes an initial value and enables the accumulation of the results of each step. This PR addsreduceTransitions(state, init, ObjTransitionFunction)
to enable iteration over the transitions of some model and accumulating the results, e.g., consider the following code change:to
In each step, the function
f
is applied to the current transition. The result of each step is the sum of all intermediate values up to the current transition.The implementation of
reduceTransitions
should not rely onforEachTransition
, as the access to a member-variable of some function (see the example above) is generally slower than to a temporary variable inreduceTransitions
. Also, as functions passed toreduce
are often clean, i.e., they do not reference external objects/variables, they could be efficiently inlined by the virtual machine (JVM does not yet apply this optimization).We could go even futher by considering
forEachTransition
a special case ofreduceTransitions
that simply dismisses the intermediate results. This should yield equal performance in theory, but would require some tests to confirm that no performance hit occurs in practice.