// ets_tracing: off import * as A from "../Collections/Immutable/Array/index.js" import * as NA from "../Collections/Immutable/NonEmptyArray/index.js" import * as O from "../Option/index.js" import { suspend } from "./core.js" import type { Effect } from "./effect.js" import { map_ } from "./map.js" import { mergeAllPar_, mergeAllParN_ } from "./mergeAll.js" import { zipWith_ } from "./zipWith.js" /** * Reduces an `Iterable[IO]` to a single `IO`, working sequentially. */ export function reduceAll_( as: NA.NonEmptyArray>, f: (acc: A, a: A) => A, __trace?: string ): Effect { return suspend( () => A.reduce_(NA.tail(as), NA.head(as), (acc, a) => zipWith_(acc, a, f)), __trace ) } /** * Reduces an `Iterable[IO]` to a single `IO`, working sequentially. * * @ets_data_first reduceAll_ */ export function reduceAll(f: (acc: A, a: A) => A, __trace?: string) { return (as: NA.NonEmptyArray>) => reduceAll_(as, f, __trace) } /** * Reduces an `Iterable[IO]` to a single `IO`, working in parallel. */ export function reduceAllPar_( as: NA.NonEmptyArray>, f: (acc: A, a: A) => A, __trace?: string ): Effect { return map_( mergeAllPar_( as, >O.none, (acc, elem) => O.some( O.fold_( acc, () => elem, (a) => f(a, elem) ) ), __trace ), O.getOrElse(() => { throw new Error("Bug") }) ) } /** * Reduces an `Iterable[IO]` to a single `IO`, working in parallel. * * @ets_data_first reduceAllPar_ */ export function reduceAllPar(f: (acc: A, a: A) => A, __trace?: string) { return (as: NA.NonEmptyArray>) => reduceAllPar_(as, f, __trace) } /** * Reduces an `Iterable[IO]` to a single `IO`, working in up to `n` fibers in parallel. */ export function reduceAllParN_( as: NA.NonEmptyArray>, n: number, f: (acc: A, a: A) => A, __trace?: string ): Effect { return map_( mergeAllParN_( as, n, >O.none, (acc, elem) => O.some( O.fold_( acc, () => elem, (a) => f(a, elem) ) ), __trace ), O.getOrElse(() => { throw new Error("Bug") }) ) } /** * Reduces an `Iterable[IO]` to a single `IO`, working in up to `n` fibers in parallel. * * @ets_data_first reduceAllParN_ */ export function reduceAllParN(n: number, f: (acc: A, a: A) => A, __trace?: string) { return (as: NA.NonEmptyArray>): Effect => reduceAllParN_(as, n, f, __trace) }