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 toReach for
Change each value inside, and keep the containerFunctor
Validate several fields and report every error, with ValidatedApplicative
Run an effect only when an earlier result calls for it, with every branch named up frontSelective
Make a call that needs the last call's resultMonad
Fail with a typed error, and recover from itMonadError
Try a cache, then the databaseAlternative
Keep only the values that pass a test, inside a ForMonadZero
Sum or count a list, with a MonoidFoldable
Look up every id, failing if any is missingTraverse

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 classKey methodsWhat it gives youPage
FunctormapChange the value inside, and keep the containerFunctor
Applicativeof, ap, map2 to map5Put a value in the container, and combine steps that do not depend on one anotherApplicative
Selectiveselect, branch, ifS, whenSRun an effect only when an earlier result calls for it, with every branch named up frontSelective
MonadflatMap, flatMap2 to flatMap5, as, peekBuild the next step from the last resultMonad
MonadErrorraiseError, handleErrorWith, handleError, recover, recoverWithFail with a typed error, and recover from itMonadError
Alternativeempty, orElse, orElseAll, guardFall back to a second container when the first is empty (Maybe), or join the two (List)Alternative
MonadZerozero, filterDrop a value that fails a test, as the when step of a For doesMonadZero
FoldablefoldMapReduce a structure to one plain value, with a MonoidFoldable and Traverse
Traversetraverse, sequenceARun an effect on every element, and gather the results in one containerFoldable and Traverse
MonadReaderask, asks, reader, localRead an environmentMTL Capabilities
MonadStateget, gets, inspect, put, modifyRead and replace a stateMTL Capabilities
MonadWritertell, listen, listens, pass, censorAdd to an output kept beside the resultMTL 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 classKey methodsWhat it gives youPage
Bifunctorbimap, first, secondChange either side of a two-sided type, such as Either or Tuple2Bifunctor
Profunctordimap, lmap, rmapAdapt a function's input and its outputProfunctor

Type classes over a plain type

These take plain values, such as two Strings, rather than a Kind.

Type classKey methodsWhat it gives youPage
SemigroupcombineJoin two values of one type, where the grouping never changes the resultSemigroup and Monoid
Monoidempty, combine, combineAll, combineN, ofA Semigroup with an empty value, so that it can fold nothing at allSemigroup and Monoid

Ready-made Semigroups and Monoids

A Semigroup joins two values:

FactoryJoins
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:

FactoryJoinsEmpty value
Monoids.list()Lists, end to endAn empty list
Monoids.set()Sets, by unionAn 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 rounds0.0
Monoids.doubleMultiplication()Doubles, by *, which rounds1.0
Monoids.bigDecimalAddition()BigDecimals, by add, exactly: use it for moneyBigDecimal.ZERO
Monoids.booleanAnd()Booleans, by &&true
Monoids.booleanOr()Booleans, by ||false
Monoids.firstOptional()Optionals, keeping the first present oneOptional.empty()
Monoids.lastOptional()Optionals, keeping the last present oneOptional.empty()
Monoids.maximum()Optionals, keeping the larger valueOptional.empty()
Monoids.maximum(comparator)Optionals, keeping the larger value by the comparatorOptional.empty()
Monoids.minimum()Optionals, keeping the smaller valueOptional.empty()
Monoids.minimum(comparator)Optionals, keeping the smaller value by the comparatorOptional.empty()
Monoids.update()Updates, applying the first and then the secondUpdate.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