forEach<E, L, R, A> static method

Effect<E, L, Iterable<R>> forEach<E, L, R, A>(
  1. Iterable<A> iterable,
  2. Effect<E, L, R> f(
    1. A a,
    2. int index
    )
)

{@category collecting}

Implementation

static Effect<E, L, Iterable<R>> forEach<E, L, R, A>(
  Iterable<A> iterable,
  Effect<E, L, R> Function(A a, int index) f,
) =>
    Effect.from(
      (context) {
        if (iterable.isEmpty) {
          return const Right([]);
        }

        return iterable
            .mapWithIndex(f)
            .fold<Effect<E, L, Iterable<R>>>(
              Effect.succeed(const Iterable.empty()),
              (acc, effect) => acc.zipWith(
                effect,
                (list, r) => list.append(r),
              ),
            )
            ._unsafeRun(context);
      },
    );