tryFold<U, E extends Object> method

Result<U, E> tryFold<U, E extends Object>(
  1. U initial,
  2. Result<U, E> f(
    1. U,
    2. T
    )
)

An iterator method that applies a function as long as it returns successfully, producing a single, final value.

Implementation

Result<U, E> tryFold<U, E extends Object>(
    U initial, Result<U, E> Function(U, T) f) {
  var accum = initial;
  for (final res in this) {
    final folded = f(accum, res);
    if (folded.isErr()) {
      return folded.intoUnchecked();
    }
    accum = folded.unwrap();
  }
  return Ok(accum);
}