Each typeclass is defined by a particular function signature and a set
of laws1 (invariants) that the typeclass must obey. 1 Typeclass laws are not listed here. See each typeclass’ scaladoc link for more information. Typeclass Signature Functor F[A] => (A => B) => F[B] Contravariant F[A] => (B => A) => F[B] Apply2 F[A] => F[A => B] => F[B] FlatMap3 F[A] => (A => F[B]) => F[B] CoFlatMap F[A] => (F[A] => B) => F[B] Traverse4 F[A] => (A => G[B]) => G[F[B]] Foldable F[A] => (B, (B, A) => B) => B SemigroupK F[A] => F[A] => F[A] Cartesian F[A] => F[B] => F[(A, B)] 2 Apply has a (broader) subtype Applicative. See the expanded ta- bles below. 3 FlatMap has a (broader) subtype Monad. 4 Traverse requires that the target type constructor G have an implicit Applicative instance available; that is, an implicit Applicative[G] must be in scope. Informally, traversing a structure maps each value to some effect, which are combined into a single effect that produces a value having the original structure. For example, by trans- forming every A of a List[A] into a Future[B], the traversal would return a Future[List[B]]. cats typeclass cheat sheet 2
Derived Functions
For each typeclass, its defining function is marked in bold and each derived function listed below it.
Typeclass Signature Function
=> (A => B) => F[B] map => (A => B) => F[(A, B)] fproduct => B => F[B] as Functor F[A] => B => F[(B, A)] tupleLeft => B => F[(A, B)] tupleRight => F[Unit] void
CoFlatMap F[A] => => F[A[A]] coflatten 5 Both the Apply and Applicative typeclasses implement the ap method; Applicative is a subtype of Apply, with an additional pure method to lift a value into the Applicative. 6 If B has a Monoid 7 If A has a Monoid
This work is licensed under a Creative Commons Attribution 4.0 International License. Issues and suggestions welcome at https://github.com/arosien/cats-cheatsheets cats typeclass cheat sheet 3
This work is licensed under a Creative Commons Attribution 4.0 International License. Issues and suggestions welcome at https://github.com/arosien/cats-cheatsheets