// ets_tracing: off import type { Associative } from "../Associative/index.js" import * as A from "../Associative/index.js" import type { Bounded } from "../Bounded/index.js" import type { Endomorphism } from "../Function/index.js" import { identity } from "../Function/index.js" import type { IdentityURI } from "../Modules/index.js" import type { Derive } from "../Prelude/Derive/index.js" import type { URI, URIS } from "../Prelude/HKT/index.js" import type { Identity } from "./definition.js" import { makeIdentity } from "./makeIdentity.js" /** * Derive `Identity` */ export function deriveIdentity( D: Derive]>, I: Identity ) { return D.derive(I) } /** * Fold `Identity` through an array */ export function fold(M: Identity): (as: ReadonlyArray) => A { const foldM = A.fold(M) return foldM(M.identity) } /** * The dual of a `Identity`, obtained by swapping the arguments of `concat`. */ export function inverted(M: Identity): Identity { return makeIdentity(M.identity, A.inverted(M).combine) } /** * `Identity` for endomorphisms */ export function endomorphism(): Identity> { return makeIdentity(identity, (x, y) => (a) => x(y(a))) } /** * `Identity` for function combination */ export function func(M: Identity): () => Identity<(a: A) => M> { return () => makeIdentity((_: A) => M.identity, A.func(M)().combine) } /** * `Identity` that returns last `Max` of elements */ export function max(B: Bounded): Identity { return makeIdentity(B.bottom, A.max(B).combine) } /** * `Identity` that returns last `Min` of elements */ export function min(B: Bounded): Identity { return makeIdentity(B.top, A.min(B).combine) } /** * Given a struct of `Identity` returns a `Identity` for the struct */ export function struct>(identities: { [K in keyof O]: Identity }): Identity { const empty: any = {} for (const key of Object.keys(identities)) { empty[key] = identities[key]!.identity } return makeIdentity(empty, A.struct(identities).combine) } /** * Given a tuple of `Identity` returns a `Identity` for the tuple */ export function tuple>>( ...identities: T ): Identity<{ [K in keyof T]: T[K] extends Associative ? A : never }> { return makeIdentity( identities.map((m) => m.identity) as any, A.tuple(...identities).combine as any ) }