Back to Effect

Introduction

packages/typeclass/README.md

latest15.4 KB
Original Source

Introduction

Welcome to the documentation for @effect/typeclass, a collection of re-usable typeclasses for the Effect ecosystem.

The functional abstractions in @effect/typeclass can be broadly divided into two categories.

  • Abstractions For Concrete Types - These abstractions define properties of concrete types, such as number and string, as well as ways of combining those values.
  • Abstractions For Parameterized Types - These abstractions define properties of parameterized types such as ReadonlyArray and Option and ways of combining them.

Concrete Types

Members and derived functions

Note: members are in bold.

Bounded

A type class used to name the lower limit and the upper limit of a type.

Extends:

  • Order
NameGivenTo
maxBoundA
minBoundA
reverseBounded<A>Bounded<A>
clampAA

Monoid

A Monoid is a Semigroup with an identity. A Monoid is a specialization of a Semigroup, so its operation must be associative. Additionally, x |> combine(empty) == empty |> combine(x) == x. For example, if we have Monoid<String>, with combine as string concatenation, then empty = "".

Extends:

  • Semigroup
NameGivenTo
emptyA
combineAllIterable<A>A
reverseMonoid<A>Monoid<A>
tuple[Monoid<A>, Monoid<B>, ...]Monoid<[A, B, ...]>
struct{ a: Monoid<A>, b: Monoid<B>, ... }Monoid<{ a: A, b: B, ... }>
minBounded<A>Monoid<A>
maxBounded<A>Monoid<A>

Semigroup

A Semigroup is any set A with an associative operation (combine):

x |> combine(y) |> combine(z) == x |> combine(y |> combine(z))

NameGivenTo
combineA, AA
combineManyA, Iterable<A>A
reverseSemigroup<A>Semigroup<A>
tuple[Semigroup<A>, Semigroup<B>, ...]Semigroup<[A, B, ...]>
struct{ a: Semigroup<A>, b: Semigroup<B>, ... }Semigroup<{ a: A, b: B, ... }>
minOrder<A>Semigroup<A>
maxOrder<A>Semigroup<A>
constantASemigroup<A>
intercalateA, Semigroup<A>Semigroup<A>
firstSemigroup<A>
lastSemigroup<A>

Parameterized Types

Parameterized Types Hierarchy

mermaid
flowchart TD
    Alternative --> SemiAlternative
    Alternative --> Coproduct
    Applicative --> Product
    Coproduct --> SemiCoproduct
    SemiAlternative --> Covariant
    SemiAlternative --> SemiCoproduct
    SemiApplicative --> SemiProduct
    SemiApplicative --> Covariant
    Applicative --> SemiApplicative
    Chainable --> FlatMap
    Chainable ---> Covariant
    Monad --> FlatMap
    Monad --> Pointed
    Pointed --> Of
    Pointed --> Covariant
    Product --> SemiProduct
    Product --> Of
    SemiProduct --> Invariant
    Covariant --> Invariant
    SemiCoproduct --> Invariant

Members and derived functions

Note: members are in bold.

Alternative

Extends:

  • SemiAlternative
  • Coproduct

Applicative

Extends:

  • SemiApplicative
  • Product
NameGivenTo
liftMonoidMonoid<A>Monoid<F<A>>

Bicovariant

A type class of types which give rise to two independent, covariant functors.

NameGivenTo
bimapF<E1, A>, E1 => E2, A => BF<E2, B>
mapLeftF<E1, A>, E1 => E2F<E2, A>
mapF<A>, A => BF<B>

Chainable

Extends:

  • FlatMap
  • Covariant
NameGivenTo
tapF<A>, A => F<B>F<A>
andThenDiscardF<A>, F<B>F<A>
bindF<A>, name: string, A => F<B>F<A & { [name]: B }>

Contravariant

Contravariant functors.

Extends:

  • Invariant
NameGivenTo
contramapF<A>, B => AF<B>
contramapCompositionF<G<A>>, A => BF<G<B>>
imapcontramapimap

Coproduct

Coproduct is a universal monoid which operates on kinds.

This type class is useful when its type parameter F<_> has a structure that can be combined for any particular type, and which also has a "zero" representation. Thus, Coproduct is like a Monoid for kinds (i.e. parametrized types).

A Coproduct<F> can produce a Monoid<F<A>> for any type A.

Here's how to distinguish Monoid and Coproduct:

  • Monoid<A> allows A values to be combined, and also means there is an "empty" A value that functions as an identity.

  • Coproduct<F> allows two F<A> values to be combined, for any A. It also means that for any A, there is an "zero" F<A> value. The combination operation and zero value just depend on the structure of F, but not on the structure of A.

Extends:

  • SemiCoproduct
NameGivenTo
zeroF<A>
coproductAllIterable<F<A>>F<A>
getMonoidMonoid<F<A>>

Covariant

Covariant functors.

Extends:

  • Invariant
NameGivenTo
mapF<A>, A => BF<B>
mapCompositionF<G<A>>, A => BF<G<B>>
imapmapimap
flapA, F<A => B>F<B>
asF<A>, BF<B>
asUnitF<A>F<void>

Filterable

Filterable<F> allows you to map and filter out elements simultaneously.

NameGivenTo
partitionMapF<A>, A => Either<B, C>[F<B>, F<C>]
filterMapF<A>, A => Option<B>F<B>
compactF<Option<A>>F<A>
separateF<Either<A, B>>[F<A>, F<B>]
filterF<A>, A => booleanF<A>
partitionF<A>, A => boolean[F<A>, F<A>]
partitionMapCompositionF<G<A>>, A => Either<B, C>[F<G<B>>, F<G<C>>]
filterMapCompositionF<G<A>>, A => Option<B>F<G<B>>

FlatMap

NameGivenTo
flatMapF<A>, A => F<B>F<B>
flattenF<F<A>>F<A>
andThenF<A>, F<B>F<B>
composeKleisliArrowA => F<B>, B => F<C>A => F<C>

Foldable

Data structures that can be folded to a summary value.

In the case of a collection (such as ReadonlyArray), these methods will fold together (combine) the values contained in the collection to produce a single result. Most collection types have reduce methods, which will usually be used by the associated Foldable<F> instance.

NameGivenTo
reduceF<A>, B, (B, A) => BB
reduceCompositionF<G<A>>, B, (B, A) => BB
reduceRightF<A>, B, (B, A) => BB
foldMapF<A>, Monoid<M>, A => MM
toReadonlyArrayF<A>ReadonlyArray<A>
toReadonlyArrayWithF<A>, A => BReadonlyArray<B>
reduceKindMonad<G>, F<A>, B, (B, A) => G<B>G<B>
reduceRightKindMonad<G>, F<A>, B, (B, A) => G<B>G<B>
foldMapKindCoproduct<G>, F<A>, (A) => G<B>G<B>

Invariant

Invariant functors.

NameGivenTo
imapF<A>, A => B, B => AF<B>
imapCompositionF<G<A>>, A => B, B => AF<G<B>>
bindToF<A>, name: stringF<{ [name]: A }>
tupledF<A>F<[A]>

Monad

Allows composition of dependent effectful functions.

Extends:

  • FlatMap
  • Pointed

Of

NameGivenTo
ofAF<A>
ofCompositionAF<G<A>>
unitF<void>
DoF<{}>

Pointed

Extends:

  • Covariant
  • Of

Product

Extends:

  • SemiProduct
  • Of
NameGivenTo
productAllIterable<F<A>>F<ReadonlyArray<A>>
tuple[F<A>, F<B>, ...]F<[A, B, ...]>
struct{ a: F<A>, b: F<B>, ... }F<{ a: A, b: B, ... }>

SemiAlternative

Extends:

  • SemiCoproduct
  • Covariant

SemiApplicative

Extends:

  • SemiProduct
  • Covariant
NameGivenTo
liftSemigroupSemigroup<A>Semigroup<F<A>>
apF<A => B>, F<A>F<B>
andThenDiscardF<A>, F<B>F<A>
andThenF<A>, F<B>F<B>
lift2(A, B) => C(F<A>, F<B>) => F<C>
lift3(A, B, C) => D(F<A>, F<B>, F<C>) => F<D>

SemiCoproduct

SemiCoproduct is a universal semigroup which operates on kinds.

This type class is useful when its type parameter F<_> has a structure that can be combined for any particular type. Thus, SemiCoproduct is like a Semigroup for kinds (i.e. parametrized types).

A SemiCoproduct<F> can produce a Semigroup<F<A>> for any type A.

Here's how to distinguish Semigroup and SemiCoproduct:

  • Semigroup<A> allows two A values to be combined.

  • SemiCoproduct<F> allows two F<A> values to be combined, for any A. The combination operation just depends on the structure of F, but not the structure of A.

Extends:

  • Invariant
NameGivenTo
coproductF<A>, F<B>F<A | B>
coproductManyIterable<F<A>>F<A>
getSemigroupSemigroup<F<A>>
coproductEitherF<A>, F<B>F<Either<A, B>>

SemiProduct

Extends:

  • Invariant
NameGivenTo
productF<A>, F<B>F<[A, B]>
productManyF<A>, Iterable<F<A>>F<[A, ...ReadonlyArray<A>]>
productCompositionF<G<A>>, F<G<B>>F<G<[A, B]>>
productManyCompositionF<G<A>>, Iterable<F<G<A>>>F<G<[A, ...ReadonlyArray<A>]>>
nonEmptyTuple[F<A>, F<B>, ...]F<[A, B, ...]>
nonEmptyStruct{ a: F<A>, b: F<B>, ... }F<{ a: A, b: B, ... }>
andThenBindF<A>, name: string, F<B>F<A & { [name]: B }>
productFlattenF<A>, F<B>F<[...A, B]>

Traversable

Traversal over a structure with an effect.

NameGivenTo
traverseApplicative<F>, T<A>, A => F<B>F<T<B>>
traverseCompositionApplicative<F>, T<G<A>>, A => F<B>F<T<G<B>>>
sequenceApplicative<F>, T<F<A>>F<T<A>>
traverseTapApplicative<F>, T<A>, A => F<B>F<T<A>>

TraversableFilterable

TraversableFilterable, also known as Witherable, represents list-like structures that can essentially have a traverse and a filter applied as a single combined operation (traverseFilter).

NameGivenTo
traversePartitionMapApplicative<F>, T<A>, A => F<Either<B, C>>F<[T<B>, T<C>]>
traverseFilterMapApplicative<F>, T<A>, A => F<Option<B>>F<T<B>>
traverseFilterApplicative<F>, T<A>, A => F<boolean>F<T<A>>
traversePartitionApplicative<F>, T<A>, A => F<boolean>F<[T<A>, T<A>]>

Adapted from: