import * as E from "../../Either"; import { pipe } from "../../Function"; import type { XRef } from "./model"; /** * Transforms both the `set` and `get` values of the `XRef` with the * specified fallible functions. */ export const bimapEither = (f: (_: C) => E.Either, g: (_: B) => E.Either) => ( _: XRef ): XRef => _.fold( (ea: EA | EC) => ea, (eb: EB | ED) => eb, f, g ); /** * Transforms both the `set` and `get` values of the `XRef` with the * specified fallible functions. */ export const bimapEither_: ( _: XRef, f: (_: C) => E.Either, g: (_: B) => E.Either ) => XRef = (_, f, g) => bimapEither(f, g)(_); /** * Transforms both the `set` and `get` values of the `XRef` with the * specified functions. */ export const bimap = (f: (_: C) => A, g: (_: B) => D) => ( _: XRef ): XRef => pipe( _, bimapEither( (c) => E.right(f(c)), (b) => E.right(g(b)) ) ); /** * Transforms both the `set` and `get` values of the `XRef` with the * specified functions. */ export const bimap_ = (_: XRef, f: (_: C) => A, g: (_: B) => D): XRef => bimap(f, g)(_); /** * Transforms both the `set` and `get` errors of the `XRef` with the * specified functions. */ export const bimapError: ( f: (_: EA) => EC, g: (_: EB) => ED ) => (_: XRef) => XRef = (f, g) => (_) => _.fold(f, g, E.right, E.right); /** * Transforms both the `set` and `get` errors of the `XRef` with the * specified functions. */ export const bimapError_: ( _: XRef, f: (_: EA) => EC, g: (_: EB) => ED ) => XRef = (_, f, g) => bimapError(f, g)(_);