foldRightWithIndex<A> method

A foldRightWithIndex<A>(
  1. Order<K> order,
  2. A initial,
  3. A compose(
    1. V value,
    2. A acc,
    3. int index
    )
)

Apply compose to all the values of this Map sorted based on the inverse of order on their key, passing also the current index of the iteration, and return the result of combining all the intermediate values.

Implementation

A foldRightWithIndex<A>(
  Order<K> order,
  A initial,
  A Function(V value, A acc, int index) compose,
) {
  final sorted = toSortedList(order).toList().reversed;
  var result = initial;
  var i = 0;
  for (final item in sorted) {
    result = compose(item.value, result, i);
    i += 1;
  }
  return result;
}