Quick Reference
Choose a type class, look up its key methods and page, and find a ready-made Semigroup or Monoid.
Which type class?
| You want to | Reach for |
|---|---|
| Change each value inside, and keep the container | Functor |
Validate several fields and report every error, with Validated | Applicative |
| Run an effect only when an earlier result calls for it, with every branch named up front | Selective |
| Make a call that needs the last call's result | Monad |
| Fail with a typed error, and recover from it | MonadError |
| Try a cache, then the database | Alternative |
Keep only the values that pass a test, inside a For | MonadZero |
Sum or count a list, with a Monoid | Foldable |
| Look up every id, failing if any is missing | Traverse |
Applicative, Selective or Monad? decides between Applicative, Selective and Monad. Behaviour belongs to the instance, not the type class: Either's ap stops at the first error, while Validated's joins them all with its Semigroup.
Type classes over a container
These work on a container held as a Kind, as How the Encoding Works explains, and the Instances facade gives you one for a type. On an Effect Path, the same operations are methods of the Path itself.
| Type class | Key methods | What it gives you | Page |
|---|---|---|---|
Functor | map | Change the value inside, and keep the container | Functor |
Applicative | of, ap, map2 to map5 | Put a value in the container, and combine steps that do not depend on one another | Applicative |
Selective | select, branch, ifS, whenS | Run an effect only when an earlier result calls for it, with every branch named up front | Selective |
Monad | flatMap, flatMap2 to flatMap5, as, peek | Build the next step from the last result | Monad |
MonadError | raiseError, handleErrorWith, handleError, recover, recoverWith | Fail with a typed error, and recover from it | MonadError |
Alternative | empty, orElse, orElseAll, guard | Fall back to a second container when the first is empty (Maybe), or join the two (List) | Alternative |
MonadZero | zero, filter | Drop a value that fails a test, as the when step of a For does | MonadZero |
Foldable | foldMap | Reduce a structure to one plain value, with a Monoid | Foldable and Traverse |
Traverse | traverse, sequenceA | Run an effect on every element, and gather the results in one container | Foldable and Traverse |
MonadReader | ask, asks, reader, local | Read an environment | MTL Capabilities |
MonadState | get, gets, inspect, put, modify | Read and replace a state | MTL Capabilities |
MonadWriter | tell, listen, listens, pass, censor | Add to an output kept beside the result | MTL Capabilities |
Argument order varies: map(f, fa) and flatMap(f, ma) take the function first, while map2(fa, fb, f) and handleErrorWith(ma, handler) take it last. Each linked page, apart from MTL Capabilities, states its type class's laws, and the hierarchy shows which type class extends which, drawn from hkj-api itself. Lifting the Hood says what a call through the encoding costs.
Type classes over a two-sided type
These work on a type with two parameters, held as a Kind2.
| Type class | Key methods | What it gives you | Page |
|---|---|---|---|
Bifunctor | bimap, first, second | Change either side of a two-sided type, such as Either or Tuple2 | Bifunctor |
Profunctor | dimap, lmap, rmap | Adapt a function's input and its output | Profunctor |
Type classes over a plain type
These take plain values, such as two Strings, rather than a Kind.
| Type class | Key methods | What it gives you | Page |
|---|---|---|---|
Semigroup | combine | Join two values of one type, where the grouping never changes the result | Semigroup and Monoid |
Monoid | empty, combine, combineAll, combineN, of | A Semigroup with an empty value, so that it can fold nothing at all | Semigroup and Monoid |
Ready-made Semigroups and Monoids
A Semigroup joins two values:
| Factory | Joins |
|---|---|
Semigroups.list() | Two lists, end to end, into a new list |
Semigroups.set() | Two sets, by union |
Semigroups.string() | Two strings, end to end |
Semigroups.string(delimiter) | Two strings, with the delimiter between them |
Semigroups.first() | Two values, keeping the first |
Semigroups.last() | Two values, keeping the second |
A Monoid adds an empty value, which changes nothing it is joined to, and which a fold of an empty structure gives:
| Factory | Joins | Empty value |
|---|---|---|
Monoids.list() | Lists, end to end | An empty list |
Monoids.set() | Sets, by union | An empty set |
Monoids.string() | Strings, end to end | "" |
Monoids.integerAddition() | Integers, by + | 0 |
Monoids.integerMultiplication() | Integers, by * | 1 |
Monoids.longAddition() | Longs, by + | 0L |
Monoids.longMultiplication() | Longs, by * | 1L |
Monoids.doubleAddition() | Doubles, by +, which rounds | 0.0 |
Monoids.doubleMultiplication() | Doubles, by *, which rounds | 1.0 |
Monoids.bigDecimalAddition() | BigDecimals, by add, exactly: use it for money | BigDecimal.ZERO |
Monoids.booleanAnd() | Booleans, by && | true |
Monoids.booleanOr() | Booleans, by || | false |
Monoids.firstOptional() | Optionals, keeping the first present one | Optional.empty() |
Monoids.lastOptional() | Optionals, keeping the last present one | Optional.empty() |
Monoids.maximum() | Optionals, keeping the larger value | Optional.empty() |
Monoids.maximum(comparator) | Optionals, keeping the larger value by the comparator | Optional.empty() |
Monoids.minimum() | Optionals, keeping the smaller value | Optional.empty() |
Monoids.minimum(comparator) | Optionals, keeping the smaller value by the comparator | Optional.empty() |
Monoids.update() | Updates, applying the first and then the second | Update.identity() |
Monoid.of(empty, combine) makes your own from an empty value and a Semigroup, as Semigroup and Monoid does to count the units of each SKU.
Previous: Supported Types Next: Foundations FAQ