Skip to content

Commit

Permalink
Merge pull request scala#10869 from ayushworks/patch-1
Browse files Browse the repository at this point in the history
Update type-classes.md
  • Loading branch information
OlivierBlanvillain authored Dec 21, 2020
2 parents c1f36f4 + dca2a57 commit 46290f9
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
3 changes: 2 additions & 1 deletion docs/docs/reference/contextual/derivation.md
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,8 @@ trait Eq[T] {
}

object Eq {
given Eq[Int] {

given Eq[Int] with {
def eqv(x: Int, y: Int) = x == y
}

Expand Down
11 changes: 5 additions & 6 deletions docs/docs/reference/contextual/type-classes.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ trait Monoid[T] extends SemiGroup[T]:
An implementation of this `Monoid` type class for the type `String` can be the following:

```scala
given Monoid[String]:
given Monoid[String] with
extension (x: String) def combine (y: String): String = x.concat(y)
def unit: String = ""
```

Whereas for the type `Int` one could write the following:

```scala
given Monoid[Int]:
given Monoid[Int] with
extension (x: Int) def combine (y: Int): Int = x + y
def unit: Int = 0
```
Expand Down Expand Up @@ -76,9 +76,9 @@ Which could read as follows: "A `Functor` for the type constructor `F[_]` repres
This way, we could define an instance of `Functor` for the `List` type:

```scala
given Functor[List]:
given Functor[List] with
def map[A, B](x: List[A], f: A => B): List[B] =
x.map(f) // List already has a `map` method
x.map(f) // List already has a `map` method
```

With this `given` instance in scope, everywhere a `Functor` is expected, the compiler will accept a `List` to be used.
Expand Down Expand Up @@ -108,11 +108,10 @@ trait Functor[F[_]]:
The instance of `Functor` for `List` now becomes:

```scala
given Functor[List]:
given Functor[List] with
extension [A, B](xs: List[A])
def map(f: A => B): List[B] =
xs.map(f) // List already has a `map` method

```

It simplifies the `assertTransformation` method:
Expand Down

0 comments on commit 46290f9

Please sign in to comment.