<commit_before>---
layout: doc-page
title: "Implementing Typeclasses"
---

Implied instance definitions, extension methods and context bounds
allow a concise and natural expression of _typeclasses_. Typeclasses are just traits
with canonical implementations defined by implied instance definitions. Here are some examples of standard typeclasses:

### Semigroups and monoids:

```scala
trait SemiGroup[T] {
  def (x: T) combine (y: T): T
}
trait Monoid[T] extends SemiGroup[T] {
  def unit: T
}
object Monoid {
  def apply[T] = the[Monoid[T]]
}

implied for Monoid[String] {
  def (x: String) combine (y: String): String = x.concat(y)
  def unit: String = ""
}

implied for Monoid[Int] {
  def (x: Int) combine (y: Int): Int = x + y
  def unit: String = 0<commit_msg>Update typeclasses.md<commit_after>---
layout: doc-page
title: "Implementing Typeclasses"
---

Implied instance definitions, extension methods and context bounds
allow a concise and natural expression of _typeclasses_. Typeclasses are just traits
with canonical implementations defined by implied instance definitions. Here are some examples of standard typeclasses:

### Semigroups and monoids:

```scala
trait SemiGroup[T] {
  def (x: T) combine (y: T): T
}
trait Monoid[T] extends SemiGroup[T] {
  def unit: T
}
object Monoid {
  def apply[T] given Monoid[T] = the[Monoid[T]]
}

implied for Monoid[String] {
  def (x: String) combine (y: String): String = x.concat(y)
  def unit: String = ""
}

implied for Monoid[Int] {
  def (x: Int) combine (y: Int): Int = x + y
  def unit: String = 0
{'fraction_non_alphanumeric': 0.1110397946084724, 'fraction_numerical': 0.0012836970474967907, 'mean_word_length': 4.3944636678200695, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1681625', 'n_tokens_mistral': 501, 'n_tokens_neox': 492, 'n_words': 194}