traverseIterable<R, E, A, B> static method

ZIO<R, E, IList<B>> traverseIterable<R, E, A, B>(
  1. Iterable<A> iterable,
  2. ZIO<R, E, B> f(
    1. A _
    )
)

Traverse an Iterable with the given function, collecting the results.

Implementation

static ZIO<R, E, IList<B>> traverseIterable<R, E, A, B>(
  Iterable<A> iterable,
  ZIO<R, E, B> Function(A _) f,
) =>
    ZIO.from((ctx) {
      if (iterable.isEmpty) {
        return Exit.right(IList());
      }

      return iterable
          .map((a) => f(a))
          .fold<ZIO<R, E, IList<B>>>(
            ZIO.succeed(IList()),
            (acc, zio) => acc.zipWith(zio, (a, B b) => a.add(b)),
          )
          .unsafeRun(ctx);
    });