A collection of reusable typeclasses for the Effect ecosystem
npm install @effect/typeclassWelcome 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.
Note: members are in bold.
A type class used to name the lower limit and the upper limit of a type.
Extends:
- Order
| Name | Given | To |
| ------------ | ------------ | ------------ |
| maxBound | | A |
| minBound | | A |
| reverse | Bounded | Bounded |
| clamp | A | A |
A Monoid is a Semigroup with an identity. A Monoid is a specialization of aSemigroup, so its operation must be associative. Additionally,x |> combine(empty) == empty |> combine(x) == x. For example, if we have Monoid,
with combine as string concatenation, then empty = "".
Extends:
- Semigroup
| Name | Given | To |
| -------------- | ------------------------------------- | ----------------------------- |
| empty | | A |
| combineAll | Iterable | A |
| reverse | Monoid | Monoid |
| tuple | [Monoid, Monoid, ...] | Monoid<[A, B, ...]> |
| struct | { a: Monoid, b: Monoid, ... } | Monoid<{ a: A, b: B, ... }> |
| min | Bounded | Monoid |
| max | Bounded | Monoid |
A Semigroup is any set A with an associative operation (combine):
x |> combine(y) |> combine(z) == x |> combine(y |> combine(z))
| Name | Given | To |
| --------------- | ------------------------------------------- | -------------------------------- |
| combine | A, A | A |
| combineMany | A, Iterable | A |
| reverse | Semigroup | Semigroup |
| tuple | [Semigroup, Semigroup, ...] | Semigroup<[A, B, ...]> |
| struct | { a: Semigroup, b: Semigroup, ... } | Semigroup<{ a: A, b: B, ... }> |
| min | Order | Semigroup |
| max | Order | Semigroup |
| constant | A | Semigroup |
| intercalate | A, Semigroup | Semigroup |
| first | | Semigroup |
| last | | Semigroup |
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
Note: members are in bold.
Extends:
- SemiAlternativeCoproduct
-
Extends:
- SemiApplicativeProduct
-
| Name | Given | To |
| ---------- | ----------- | -------------- |
| liftMonoid | Monoid | Monoid |
A type class of types which give rise to two independent, covariant
functors.
| Name | Given | To |
| --------- | -------------------------------- | ---------- |
| bimap | F, E1 => E2, A => B | F |F
| mapLeft | , E1 => E2 | F |F
| map | , A => B | F |
Extends:
- FlatMapCovariant
-
| Name | Given | To |
| -------------- | ----------------------------------- | ---------------------- |
| tap | F, A => F | F |F
| andThenDiscard | , F | F |F
| bind | , name: string, A => F | F |
Contravariant functors.
Extends:
- Invariant
| Name | Given | To |
| -------------------- | ------------------- | --------- |
| contramap | F, B => A | F |F
| contramapComposition | , A => B | F |contramap
| imap | | imap |
Coproduct is a universal monoid which operates on kinds.
This type class is useful when its type parameter F<_> has aCoproduct
structure that can be combined for any particular type, and which
also has a "zero" representation. Thus, is like a Monoid
for kinds (i.e. parametrized types).
A Coproduct can produce a Monoid for any type A.
Here's how to distinguish Monoid and Coproduct:
- Coproduct allows two F values to be combined, for any A. ItA
also means that for any , there is an "zero" F value. TheF
combination operation and zero value just depend on the
structure of , but not on the structure of A.
Extends:
- SemiCoproduct
| Name | Given | To |
| ---------------- | ---------------- | -------------- |
| zero | | F |Iterable
| coproductAll | | F |Monoid
| getMonoid | | |
Covariant functors.
Extends:
- Invariant
| Name | Given | To |
| -------------- | ------------------- | --------- |
| map | F, A => B | F |F
| mapComposition | , A => B | F |map
| imap | | imap |A
| flap | , F B> | F |F
| as | , B | F |F
| asUnit | | F |
Filterable allows you to map and filter out elements simultaneously.
| Name | Given | To |
| ----------------------- | ------------------------------ | -------------------- |
| partitionMap | F, A => Either | [F, F |F
| filterMap | , A => Option | F |F
| compact |
| Name | Given | To |
| ------------------- | ------------------------ | ----------- |
| flatMap | F, A => F | F |F
| flatten | | F |F
| andThen | , F | F |A => F
| composeKleisliArrow | , B => F | A => F |
Data structures that can be folded to a summary value.
In the case of a collection (such as ReadonlyArray), thesereduce
methods will fold together (combine) the values contained in the
collection to produce a single result. Most collection types have methods, which will usually be used by the associatedFoldable instance.
| Name | Given | To |
| ------------------- | ----------------------------------------- | ------------------ |
| reduce | F, B, (B, A) => B | B |F
| reduceComposition | , B, (B, A) => B | B |F
| reduceRight | , B, (B, A) => B | B |F
| foldMap | , Monoid, A => M | M |F
| toReadonlyArray | | ReadonlyArray |F
| toReadonlyArrayWith | , A => B | ReadonlyArray |Monad
| reduceKind | , F, B, (B, A) => G | G |Monad
| reduceRightKind | , F, B, (B, A) => G | G |Coproduct
| foldMapKind | , F, (A) => G | G |
Invariant functors.
| Name | Given | To |
| --------------- | ----------------------------- | ------------------ |
| imap | F, A => B, B => A | F |F
| imapComposition | , A => B, B => A | F |F
| bindTo | , name: string | F<{ [name]: A }> |F
| tupled | | F<[A]> |
Allows composition of dependent effectful functions.
Extends:
- FlatMapPointed
-
| Name | Given | To |
| ------------- | ----- | --------- |
| of | A | F |A
| ofComposition | | F |F
| unit | | |F<{}>
| Do | | |
Extends:
- CovariantOf
-
Extends:
- SemiProductOf
-
| Name | Given | To |
| -------------- | --------------------------- | ------------------------ |
| productAll | Iterable | F |[F, F, ...]
| tuple | | F<[A, B, ...]> |{ a: F, b: F, ... }
| struct | | F<{ a: A, b: B, ... }> |
Extends:
- SemiCoproductCovariant
-
Extends:
- SemiProductCovariant
-
| Name | Given | To |
| -------------- | ------------------- | ---------------------------- |
| liftSemigroup | Semigroup | Semigroup |F B>
| ap | , F | F |F
| andThenDiscard | , F | F |F
| andThen | , F | F |(A, B) => C
| lift2 | | (F, F) => F |(A, B, C) => D
| lift3 | | (F, F, F |
SemiCoproduct is a universal semigroup which operates on kinds.
This type class is useful when its type parameter F<_> has aSemiCoproduct
structure that can be combined for any particular type. Thus, is like a Semigroup for kinds (i.e. parametrized
types).
A SemiCoproduct can produce a Semigroup for any type A.
Here's how to distinguish Semigroup and SemiCoproduct:
- Semigroup allows two A values to be combined.
- SemiCoproduct allows two F values to be combined, for any A.F
The combination operation just depends on the structure of ,A
but not the structure of .
Extends:
- Invariant
| Name | Given | To |
| ----------------- | ---------------- | ----------------- |
| coproduct | F, F | F |Iterable
| coproductMany | | F |Semigroup
| getSemigroup | | |F
| coproductEither | , F | F |
Extends:
- Invariant
| Name | Given | To |
| ---------------------- | ------------------------------ | -------------------------------- |
| product | F, F | F<[A, B]> |F
| productMany | , Iterable | F<[A, ...ReadonlyArray]> |F
| productComposition | , F | F |F
| productManyComposition | , Iterable | F |[F, F, ...]
| nonEmptyTuple | | F<[A, B, ...]> |{ a: F, b: F, ... }
| nonEmptyStruct | | F<{ a: A, b: B, ... }> |F
| andThenBind | , name: string, F | F |F
| productFlatten | , F | F<[...A, B]> |
Traversal over a structure with an effect.
| Name | Given | To |
| ------------------- | ---------------------------------------- | ------------ |
| traverse | Applicative, T, A => F | F |Applicative
| traverseComposition | , T, A => F | F |Applicative
| sequence | , T | F |Applicative
| traverseTap | , T, A => F | F |
TraversableFilterable, also known as Witherable, represents list-like structurestraverse
that can essentially have a and a filter applied as a singletraverseFilter
combined operation ().
| Name | Given | To |
| ------------------------ | ------------------------------------------------ | ----------------- |
| traversePartitionMap | Applicative, T, A => F | F<[T, T |Applicative
| traverseFilterMap | , T, A => F
---
Adapted from: