traverseListWithIndex<S, A, B> static method

State<S, List<B>> traverseListWithIndex<S, A, B>(
  1. List<A> list,
  2. State<S, B> f(
    1. A a,
    2. int i
    )
)

Map each element in the list to a State using the function f, and collect the result in a State<S, List<B>>.

Same as State.traverseList but passing index in the map function.

Implementation

static State<S, List<B>> traverseListWithIndex<S, A, B>(
    List<A> list, State<S, B> Function(A a, int i) f) {
  return State((state) {
    final resultList = <B>[];
    var out = state;
    for (var i = 0; i < list.length; i++) {
      final (b, s) = f(list[i], i).run(out);
      resultList.add(b);
      out = s;
    }
    return (resultList, out);
  });
}