// ets_tracing: off
import * as Chunk from "../Collections/Immutable/Chunk/core.js"
import * as E from "../Either/index.js"
import { absolve } from "./absolve.js"
import type { Effect } from "./effect.js"
import { either } from "./either.js"
import { forEach_, forEachExec_, forEachPar_, forEachParN_ } from "./excl-forEach.js"
import type { ExecutionStrategy } from "./ExecutionStrategy.js"
import { map_ } from "./map.js"
/**
* Feeds elements of type `A` to `f` and accumulates all errors in error
* channel or successes in success channel.
*
* This combinator is lossy meaning that if there are errors all successes
* will be lost.
*/
export function validate_(
as: Iterable,
f: (a: A) => Effect,
__trace?: string
) {
return absolve(
map_(
forEach_(as, (a) => either(f(a))),
mergeExits()
),
__trace
)
}
/**
* Feeds elements of type `A` to `f` and accumulates all errors in error
* channel or successes in success channel.
*
* This combinator is lossy meaning that if there are errors all successes
* will be lost.
*/
export function validatePar_(
as: Iterable,
f: (a: A) => Effect,
__trace?: string
) {
return absolve(
map_(
forEachPar_(as, (a) => either(f(a))),
mergeExits()
),
__trace
)
}
/**
* Feeds elements of type `A` to `f` and accumulates all errors in error
* channel or successes in success channel.
*
* This combinator is lossy meaning that if there are errors all successes
* will be lost.
*/
export function validateParN_(
as: Iterable,
n: number,
f: (a: A) => Effect,
__trace?: string
) {
return absolve(
map_(
forEachParN_(as, n, (a) => either(f(a))),
mergeExits()
),
__trace
)
}
function mergeExits(): (
a: Chunk.Chunk>
) => E.Either, Chunk.Chunk> {
return (exits) => {
let errors = Chunk.empty()
let results = Chunk.empty()
for (const e of exits) {
if (e._tag === "Left") {
errors = Chunk.append_(errors, e.left)
} else {
results = Chunk.append_(results, e.right)
}
}
if (!Chunk.isEmpty(errors)) {
return E.left(errors)
} else {
return E.right(results)
}
}
}
/**
* Feeds elements of type `A` to `f` and accumulates all errors in error
* channel or successes in success channel.
*
* This combinator is lossy meaning that if there are errors all successes
* will be lost.
*/
export function validateExec_(
as: Iterable,
es: ExecutionStrategy,
f: (a: A) => Effect,
__trace?: string
): Effect, Chunk.Chunk> {
return absolve(
map_(
forEachExec_(as, es, (a) => either(f(a))),
mergeExits()
),
__trace
)
}
/**
* Feeds elements of type `A` to `f` and accumulates all errors in error
* channel or successes in success channel.
*
* This combinator is lossy meaning that if there are errors all successes
* will be lost.
*
* @ets_data_first validate_
*/
export function validate(f: (a: A) => Effect, __trace?: string) {
return (as: Iterable) => validate_(as, f, __trace)
}
/**
* Feeds elements of type `A` to `f` and accumulates all errors in error
* channel or successes in success channel.
*
* This combinator is lossy meaning that if there are errors all successes
* will be lost.
*
* @ets_data_first validatePar_
*/
export function validatePar(
f: (a: A) => Effect,
__trace?: string
) {
return (as: Iterable) => validatePar_(as, f, __trace)
}
/**
* Feeds elements of type `A` to `f` and accumulates all errors in error
* channel or successes in success channel.
*
* This combinator is lossy meaning that if there are errors all successes
* will be lost.
*
* @ets_data_first validateParN_
*/
export function validateParN(
n: number,
f: (a: A) => Effect,
__trace?: string
) {
return (as: Iterable) => validateParN_(as, n, f, __trace)
}
/**
* Feeds elements of type `A` to `f` and accumulates all errors in error
* channel or successes in success channel.
*
* This combinator is lossy meaning that if there are errors all successes
* will be lost.
*
* @ets_data_first validateExec_
*/
export function validateExec(
es: ExecutionStrategy,
f: (a: A) => Effect,
__trace?: string
): (as: Iterable) => Effect, Chunk.Chunk> {
return (as) => validateExec_(as, es, f, __trace)
}