import * as A from "../../Array"; import { pipe } from "../../Function"; import * as O from "../../Option"; import { mapBothCause } from "./apply"; import { first_ } from "./bifunctor"; import * as C from "./Cause"; import { map, map_ } from "./functor"; import type { Exit } from "./model"; export const as_ = (fa: Exit, b: B): Exit => map_(fa, () => b); export const as = (b: B): ((fa: Exit) => Exit) => map(() => b); export const sequenceA = (...exits: ReadonlyArray>): O.Option>> => pipe( A.head(exits), O.map((head) => pipe( A.dropLeft_(exits, 1), A.reduce( pipe( head, map((x): ReadonlyArray => [x]) ), (acc, el) => pipe( acc, mapBothCause(el, (acc, el) => [el, ...acc], C.then) ) ), map(A.reverse) ) ) ); export const sequenceAPar = (...exits: ReadonlyArray>): O.Option> => pipe( A.head(exits), O.map((head) => pipe( A.dropLeft_(exits, 1), A.reduce( pipe( head, map((x): ReadonlyArray => [x]) ), (acc, el) => pipe( acc, mapBothCause(el, (acc, el) => [el, ...acc], C.both) ) ), map(A.reverse) ) ) ); export const orElseFail_ = (exit: Exit, orElse: G) => first_(exit, () => orElse); export const orElseFail = (orElse: G) => (exit: Exit): Exit => orElseFail_(exit, orElse);