import { mapM_ } from "../_core"; import * as T from "../_internal/task"; import { tuple } from "../../../Function"; import { parallel, sequential } from "../../ExecutionStrategy"; import type { Managed } from "../model"; import { makeManagedReleaseMap } from "./makeManagedReleaseMap"; /** * Applies the function `f` to each element of the `Iterable` in parallel, * and returns the results in a new `B[]`. * * For a sequential version of this method, see `foreach`. */ export const traverseIPar = (f: (a: A) => Managed) => ( as: Iterable ): Managed => traverseIPar_(as, f); /** * Applies the function `f` to each element of the `Iterable` in parallel, * and returns the results in a new `B[]`. * * For a sequential version of this method, see `foreach_`. */ export const traverseIPar_ = ( as: Iterable, f: (a: A) => Managed ): Managed => mapM_(makeManagedReleaseMap(parallel()), (parallelReleaseMap) => { const makeInnerMap = T.local_( T.map_(makeManagedReleaseMap(sequential()).task, ([_, x]) => x), (x: unknown) => tuple(x, parallelReleaseMap) ); return T.traverseIPar_(as, (a) => T.map_( T.chain_(makeInnerMap, (innerMap) => T.local_(f(a).task, (u: R) => tuple(u, innerMap))), ([_, b]) => b ) ); });