Skip to main content Link Search Menu Expand Document (external link)

Covariant overview

Added in v0.24.0


Table of contents


do notation

let

Signature

export declare const let: <F extends TypeLambda>(
  F: Covariant<F>
) => {
  <N extends string, A extends object, B>(
    name: Exclude<N, keyof A>,
    f: (a: A) => B
  ): <R, O, E>(self: Kind<F, R, O, E, A>) => Kind<F, R, O, E, { [K in keyof A | N]: K extends keyof A ? A[K] : B }>
  <R, O, E, A extends object, N extends string, B>(
    self: Kind<F, R, O, E, A>,
    name: Exclude<N, keyof A>,
    f: (a: A) => B
  ): Kind<F, R, O, E, { [K in keyof A | N]: K extends keyof A ? A[K] : B }>
}

Example

import * as covariant from "@effect/typeclass/Covariant"
import type { HKT } from "effect"
import { dual, pipe } from "effect/Function"

interface MyData<A> {
  readonly value: A
}

interface MyDataTypeLambda extends HKT.TypeLambda {
  readonly type: MyData<this["Target"]>
}

const map: covariant.Covariant<MyDataTypeLambda>["map"] = dual(
  2,
  <A, B>(self: MyData<A>, f: (a: A) => B): MyData<B> => ({ value: f(self.value) })
)

const Covariant: covariant.Covariant<MyDataTypeLambda> = { imap: covariant.imap<MyDataTypeLambda>(map), map }

const notation = {
  Do: { value: {} } as MyData<{}>,
  let: covariant.let(Covariant)
}

assert.deepStrictEqual(
  pipe(
    notation.Do,
    notation.let("foo", () => "bar")
  ),
  { value: { foo: "bar" } }
)

Added in v0.24.0

mapping

as

Signature

export declare const as: <F extends TypeLambda>(
  F: Covariant<F>
) => {
  <B>(b: B): <R, O, E, _>(self: Kind<F, R, O, E, _>) => Kind<F, R, O, E, B>
  <R, O, E, _, B>(self: Kind<F, R, O, E, _>, b: B): Kind<F, R, O, E, B>
}

Added in v0.24.0

asVoid

Signature

export declare const asVoid: <F extends TypeLambda>(
  F: Covariant<F>
) => <R, O, E, _>(self: Kind<F, R, O, E, _>) => Kind<F, R, O, E, void>

Added in v0.24.0

flap

Signature

export declare const flap: <F extends TypeLambda>(
  F: Covariant<F>
) => {
  <R, O, E, A, B>(self: Kind<F, R, O, E, (a: A) => B>): (a: A) => Kind<F, R, O, E, B>
  <A, R, O, E, B>(a: A, self: Kind<F, R, O, E, (a: A) => B>): Kind<F, R, O, E, B>
}

Added in v0.24.0

type class

Covariant (interface)

Signature

export interface Covariant<F extends TypeLambda> extends Invariant<F> {
  readonly map: {
    <A, B>(f: (a: A) => B): <R, O, E>(self: Kind<F, R, O, E, A>) => Kind<F, R, O, E, B>
    <R, O, E, A, B>(self: Kind<F, R, O, E, A>, f: (a: A) => B): Kind<F, R, O, E, B>
  }
}

Added in v0.24.0

utils

imap

Returns a default imap implementation.

Signature

export declare const imap: <F extends TypeLambda>(
  map: <R, O, E, A, B>(self: Kind<F, R, O, E, A>, f: (a: A) => B) => Kind<F, R, O, E, B>
) => Invariant<F>["imap"]

Added in v0.24.0

mapComposition

Returns a default map composition.

Signature

export declare const mapComposition: <F extends TypeLambda, G extends TypeLambda>(
  F: Covariant<F>,
  G: Covariant<G>
) => <FR, FO, FE, GR, GO, GE, A, B>(
  self: Kind<F, FR, FO, FE, Kind<G, GR, GO, GE, A>>,
  f: (a: A) => B
) => Kind<F, FR, FO, FE, Kind<G, GR, GO, GE, B>>

Added in v0.24.0