From e981b008542adf81f39eb4fa79bbc707d4911aa1 Mon Sep 17 00:00:00 2001 From: "Lan, Jian" Date: Tue, 15 Sep 2026 17:59:00 +0800 Subject: [PATCH 1/2] Fix some markdown style issues for typeclasses.md --- docs/typeclasses.md | 40 +++++++++++++++++++--------------------- 1 file changed, 19 insertions(+), 21 deletions(-) diff --git a/docs/typeclasses.md b/docs/typeclasses.md index 85a4d32f69..935614d668 100644 --- a/docs/typeclasses.md +++ b/docs/typeclasses.md @@ -1,10 +1,12 @@ # Type Classes + Type classes are a powerful tool used in functional programming to enable ad-hoc polymorphism, more commonly known as overloading. Where many object-oriented languages leverage subtyping for polymorphic code, functional programming tends towards a combination of parametric polymorphism (think type parameters, like Java generics) and ad-hoc polymorphism. ## Example: collapsing a list + The following code snippets show code that sums a list of integers, concatenates a list of strings, and unions a list of sets. @@ -42,6 +44,7 @@ def combineAll[A](list: List[A], m: Monoid[A]): A = list.foldRight(m.empty)(m.co ``` ## Type classes vs. subtyping + The definition above takes an actual monoid argument instead of doing the usual object-oriented practice of using subtype constraints. @@ -156,6 +159,7 @@ combineAll(List(Paired(1, "hello"), Paired(2, " "), Paired(3, "world"))) ``` ## A note on syntax + In many cases, including the `combineAll` function above, the implicit arguments can be written with syntactic sugar. ```scala mdoc:compile-only @@ -230,33 +234,27 @@ You can find out more about law testing [here](typeclasses/lawtesting.md). infographic From [cats-infographic by @tpolecat](https://github.com/tpolecat/cats-infographic). - ## Incomplete type class instances in cats Originally from [@hobwekiva](https://gist.github.com/hobwekiva/d63508ddb6a728015ace53cb70a1fd5d) - -| Type | Functor | Apply | Applicative | Monad | MonoidK | ApplicativeError | MonadError | CoflatMap | Comonad | Bimonad | -| --------------- |:-------:|:-----------------:|:-----------:|:-----:|:-------:|:-----------------:|:----------:|:---------:|:-------:|:-------:| -| `Id[A]` | ✔ | ✔ | ✔ | ✔ | ✗ | ✗ | ✗ | ✔ | ✔ |✔ | -| `Eval[A]` | ✔ | ✔ | ✔ | ✔ | ✗ | ✗ | ✗ | ✔ | ✔ |✔ | -| `Option[A]` | ✔ | ✔ | ✔ | ✔ | ✔ | ✔ | ✔ | ✔ | ✗ |✗ | -| `Const[K, A]` | ✔ | ✔ (`K:Monoid`) | ✔ | ✗ | ✗ | ✗ | ✗ | ✗ | ✗ |✗ | -| `Either[E, A]` | ✔ | ✔ | ✔ | ✔ | ✔ | ✔ | ✔ | ✗ | ✗ |✗ | -| `List[A]` | ✔ | ✔ | ✔ | ✔ | ✔ | ✗ | ✗ | ✔ | ✗ |✗ | -| `NonEmptyList[A]` | ✔ | ✔ | ✔ | ✔ | ✗ | ✗ | ✗ | ✔ | ✔ |✔ | -| `Stream[A]` | ✔ | ✔ | ✔ | ✔ | ✔ | ✗ | ✗ | ✔ | ✗ |✗ | -| `Map[K, A]` | ✔ | ✔ | ✗ | ✗ | ✔ | ✗ | ✗ | ✗ | ✗ |✗ | -| `Validated[E, A]` | ✔ | ✔ (`E: Semigroup`)| ✔ | ✗ | ✗ | ✔ (`E: Semigroup`)| ✗ | ✗ | ✗ |✗ | -| `Reader[E, A]` | ✔ | ✔ | ✔ | ✔ | ✗ | ✗ | ✗ | ✗ | ✗ |✗ | -| `Writer[E, A]` | ✔ | ✔ (`E:Monoid`) | ✔ | ✔ | ✗ | ✗ | ✗ | ✔ | ✗ |✗ | - - - +| Type | Functor | Apply | Applicative | Monad | MonoidK | ApplicativeError | MonadError | CoflatMap | Comonad | Bimonad | +| ----------------- |:-------:|:-----------------:|:-----------:|:-----:|:-------:|:-----------------:|:----------:|:---------:|:-------:|:-------:| +| `Id[A]` | ✔ | ✔ | ✔ | ✔ | ✗ | ✗ | ✗ | ✔ | ✔ |✔ | +| `Eval[A]` | ✔ | ✔ | ✔ | ✔ | ✗ | ✗ | ✗ | ✔ | ✔ |✔ | +| `Option[A]` | ✔ | ✔ | ✔ | ✔ | ✔ | ✔ | ✔ | ✔ | ✗ |✗ | +| `Const[K, A]` | ✔ | ✔ (`K:Monoid`) | ✔ | ✗ | ✗ | ✗ | ✗ | ✗ | ✗ |✗ | +| `Either[E, A]` | ✔ | ✔ | ✔ | ✔ | ✔ | ✔ | ✔ | ✗ | ✗ |✗ | +| `List[A]` | ✔ | ✔ | ✔ | ✔ | ✔ | ✗ | ✗ | ✔ | ✗ |✗ | +| `NonEmptyList[A]` | ✔ | ✔ | ✔ | ✔ | ✗ | ✗ | ✗ | ✔ | ✔ |✔ | +| `Stream[A]` | ✔ | ✔ | ✔ | ✔ | ✔ | ✗ | ✗ | ✔ | ✗ |✗ | +| `Map[K, A]` | ✔ | ✔ | ✗ | ✗ | ✔ | ✗ | ✗ | ✗ | ✗ |✗ | +| `Validated[E, A]` | ✔ | ✔ (`E: Semigroup`)| ✔ | ✗ | ✗ | ✔ (`E: Semigroup`)| ✗ | ✗ | ✗ |✗ | +| `Reader[E, A]` | ✔ | ✔ | ✔ | ✔ | ✗ | ✗ | ✗ | ✗ | ✗ |✗ | +| `Writer[E, A]` | ✔ | ✔ (`E:Monoid`) | ✔ | ✔ | ✗ | ✗ | ✗ | ✔ | ✗ |✗ | ## Further reading -* [Returning the "Current" Type in Scala][fbounds] - +* [Returning the "Current" Type in Scala][fbounds] [fbounds]: http://tpolecat.github.io/2015/04/29/f-bounds.html "Returning the "Current" Type in Scala" From 8f67029b0178461a628d3b2b45d3ec58ad660292 Mon Sep 17 00:00:00 2001 From: "Lan, Jian" Date: Tue, 15 Sep 2026 18:22:04 +0800 Subject: [PATCH 2/2] Update cats-infographic --- docs/typeclasses.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/typeclasses.md b/docs/typeclasses.md index 935614d668..a4ec759e94 100644 --- a/docs/typeclasses.md +++ b/docs/typeclasses.md @@ -231,8 +231,8 @@ You can find out more about law testing [here](typeclasses/lawtesting.md). ## Type classes in Cats -infographic -From [cats-infographic by @tpolecat](https://github.com/tpolecat/cats-infographic). +infographic +From [cats-infographic by @tpolecat, updated by @ShapelessCat](https://github.com/ShapelessCat/cats-infographic). ## Incomplete type class instances in cats