// ets_tracing: off import "../Operator/index.js" /* adapted from https://github.com/gcanti/fp-ts */ /** * The `Const` type constructor, which wraps its first type argument and ignores its second. * That is, `Const` is isomorphic to `E` for any `A`. * * `Const` has some useful instances. For example, the `Applicative` instance allows us to collect results using a `Monoid` * while ignoring return values. */ import type * as As from "../Associative/index.js" import type { Bounded } from "../Bounded/index.js" import type { Equal } from "../Equal/index.js" import { unsafeCoerce } from "../Function/index.js" import type * as Id from "../Identity/index.js" import type { ConstURI } from "../Modules/index.js" import type { Ord } from "../Ord/index.js" import type { URI } from "../Prelude/index.js" import * as P from "../Prelude/index.js" import type { Show } from "../Show/index.js" import { makeShow } from "../Show/index.js" /** * The `Const` type constructor, which wraps its first type argument and ignores its second. * That is, `Const` is isomorphic to `E` for any `A`. * * `Const` has some useful instances. For example, the `Applicative` instance allows us to collect results using a `Identity` * while ignoring return values. */ export type Const = E & { readonly _A: A } /** * Map + MapLeft */ export function bimap_( fea: Const, f: (e: E) => G, g: (a: A) => B ): Const { return makeConst(f(fea))() } /** * Map + MapLeft */ export function bimap( f: (e: E) => G, g: (a: A) => B ): (fa: Const) => Const { return (fa) => bimap_(fa, f, g) } /** * Contramap input */ export const contramap_: (fa: Const, f: (b: B) => A) => Const = unsafeCoerce /** * Contramap input */ export function contramap(f: (b: B) => A): (fa: Const) => Const { return (fa) => contramap_(fa, f) } /** * The `Any` instance for `Const[E, +_]` */ export function getAny(e: E) { return P.instance], P.Fix<"E", E>>>({ any: makeConst(e) }) } /** * The `AssociativeBoth` instance for `Const[E, +_]` */ export function getAssociativeBoth(A: As.Associative) { return P.instance], P.Fix<"E", E>>>({ both: (fb) => (fa) => makeConst(A.combine(fa, fb))() }) } /** * The `Contravariant` instance for `Const[+_, +_]` */ export const Contravariant = P.instance< P.Contravariant<[URI], P.V<"E", "+">> >({ contramap }) /** * The `Covariant` instance for `Const[E, +_]` */ export const Covariant = P.instance], P.V<"E", "+">>>({ map }) /** * The `IdentityBoth` instance for `Const[E, +_]` */ export function getIdentityBoth(I: Id.Identity) { return P.instance], P.Fix<"E", E>>>({ ...getAny(I.identity), ...getAssociativeBoth(I) }) } /** * The `Applicative` instance for `Const[E, +_]` */ export function getApplicative(I: Id.Identity) { return P.instance], P.Fix<"E", E>>>({ ...Covariant, ...getIdentityBoth(I) }) } /** * The `Show` instance for `Const[E, +_]` */ export function getShow(S: Show) { return () => makeShow>((c) => `make(${S.show(c)})`) } /** * The `Bounded` instance for `Const[E, +_]` */ export function getBounded(B: Bounded): () => Bounded> { return () => B as any } /** * The `Equal` instance for `Const[E, +_]` */ export function getEqual(E: Equal): () => Equal> { return () => E as any } /** * The `Identity` instance for `Const[E, +_]` */ export function getIdentity(I: Id.Identity): () => Id.Identity> { return () => I as any } /** * The `Ord` instance for `Const[E, +_]` */ export function getOrd(O: Ord): () => Ord> { return () => O as any } /** * The `Associative` instance for `Const[E, +_]` */ export function getAssociative( A: As.Associative ): () => As.Associative> { return () => A as any } /** * Construct `Const[E, A]` */ export const makeConst: (e: E) => () => Const = (e) => () => unsafeCoerce(e) /** * Maps `Const[E, A]` to `Const[E, B]` via `f : A => B` * * @ets_optimize identity */ export const map_: (fa: Const, f: (a: A) => B) => Const = unsafeCoerce /** * Maps `Const[E, A]` to `Const[E, B]` via `f : A => B` */ export function map( f: (a: A) => B ): { /** * @ets_optimize identity */ (fa: Const): Const } { return (fa) => map_(fa, f) } /** * Maps `Const[E, A]` to `Const[E1, A]` via `f : E => E1` */ export const mapLeft_: (fea: Const, f: (e: E) => G) => Const = ( fea, f ) => makeConst(f(fea))() /** * Maps `Const[E, A]` to `Const[E1, A]` via `f : E => E1` */ export function mapLeft( f: (e: E) => G ): { (fa: Const): Const } { return (fa) => mapLeft_(fa, f) }