Skip to content

Commit 4f284ad

Browse files
committed
Conversion of Cats chapter
- Fix links etc to Typst style
1 parent 4d2812c commit 4f284ad

File tree

5 files changed

+35
-36
lines changed

5 files changed

+35
-36
lines changed

src/pages/cats/conclusions.typ

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,20 @@
1-
#import "../stdlib.typ": info, warning, solution
1+
#import "../stdlib.typ": href
22
== Conclusions
33

4-
54
We have also seen the general patterns in Cats type classes:
65

76
- The type classes themselves are generic traits
8-
in the [`cats`][cats.package] package.
7+
in the `cats` package.
98

109
- Each type class has a companion object with,
1110
an `apply` method for materializing instances,
12-
one or more _construction_ methods for creating instances,
11+
one or more construction methods for creating instances,
1312
and a collection of other relevant helper methods.
1413

15-
- Many type classes have _syntax_
16-
provided via the [`cats.syntax`][cats.syntax] package.
14+
- Many type classes have extension methods
15+
provided via the `cats.syntax` package.
1716

18-
In the remaining chapters of Part {#sec:part:type-classes}
17+
In the remaining chapters of @sec:part:type-classes
1918
we will look at several broad and powerful type classes---`Semigroup`,
2019
`Monoid`, `Functor`, `Monad`, `Semigroupal`, `Applicative`, `Traverse`, and more.
2120
In each case we will learn what functionality the type class provides,

src/pages/cats/equal.typ

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,16 @@
1-
#import "../stdlib.typ": info, warning, solution
1+
#import "../stdlib.typ": info, warning, solution, href
22
== Example: Eq
33

44

55
We will finish off this chapter by looking at another useful type class:
6-
[`cats.Eq`][cats.kernel.Eq].
6+
#href("http://typelevel.org/cats/api/cats/kernel/Eq.html")[`cats.Eq`].
77
`Eq` is designed to support _type-safe equality_
88
and address annoyances using Scala's built-in `==` operator.
99

1010
Almost every Scala developer has written code like this before:
1111

12-
```scala
12+
```scala mdoc:fail
1313
List(1, 2, 3).map(Option(_)).filter(item => item == 1)
14-
// warning: Option[Int] and Int are unrelated: they will most likely never compare equal
15-
// res: List[Option[Int]] = List()
16-
1714
```
1815

1916
Ok, many of you won't have made such a simple mistake as this,
@@ -26,12 +23,11 @@ should have compared `item` to `Some(1)` instead of `1`.
2623
However, it's not technically a type error because
2724
`==` works for any pair of objects, no matter what types we compare.
2825
`Eq` is designed to add some type safety to equality checks
29-
and work around this problem.
26+
and work around this problem.#footnote[Scala 3 has it's own solution to this problem, called #href("https://docs.scala-lang.org/scala3/reference/contextual/multiversal-equality.html")[multiversal equality]. It also uses a type class, in this case called `CanEqual`. With the correct imports or compiler flags we can get the compiler to complain if we try to perform an equality check that doesn't make sense. So in practice we don't need `Eq` any more. However it's a simple type class to work with and makes a good introduction to using Cats.]
3027

3128

3229
=== Equality, Liberty, and Fraternity
3330

34-
3531
We can use `Eq` to define type-safe equality
3632
between instances of any given type:
3733

@@ -44,7 +40,7 @@ trait Eq[A] {
4440
}
4541
```
4642

47-
The interface syntax, defined in [`cats.syntax.eq`][cats.syntax.eq],
43+
The interface syntax, defined in #href("https://www.javadoc.io/doc/org.typelevel/cats-docs_3/latest/cats/syntax/EqSyntax.html")[`cats.syntax.eq`],
4844
provides two methods for performing equality checks
4945
provided there is an instance `Eq[A]` in scope:
5046

@@ -82,13 +78,15 @@ we get a compile error:
8278
eqInt.eqv(123, "234")
8379
```
8480

85-
We can also import the interface syntax in [`cats.syntax.eq`][cats.syntax.eq]
81+
We can also import the interface syntax
8682
to use the `===` and `=!=` methods:
8783

8884
```scala mdoc:silent
8985
import cats.syntax.all.* // for === and =!=
9086
```
9187

88+
Now the syntax methods are available.
89+
9290
```scala mdoc
9391
123 === 123
9492
123 =!= 234
@@ -126,7 +124,7 @@ the `Option.apply` and `Option.empty` methods from the standard library:
126124
Option(1) === Option.empty[Int]
127125
```
128126

129-
or using special syntax from [`cats.syntax.option`][cats.syntax.option]:
127+
or using special syntax from #href("https://www.javadoc.io/doc/org.typelevel/cats-docs_3/latest/cats/syntax/OptionSyntax.html")[`cats.syntax.option`]:
130128

131129
```scala mdoc
132130
1.some === none[Int]

src/pages/cats/index.typ

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
1-
#import "../stdlib.typ": info, warning, solution
2-
= Using Cats
3-
<sec:cats>
1+
#import "../stdlib.typ": info, warning, solution, chapter, href, exercise
2+
#chapter[Using Cats] <sec:cats>
43

5-
6-
In this Chapter we'll learn how to use the #link("https://typelevel.org/cats")[Cats] library.
4+
In this Chapter we'll learn how to use the #href("https://typelevel.org/cats")[Cats] library.
75
Cats provides two main things: type classes and their instances, and some useful data structures.
86
Our focus will mostly be on the type classes, though we will touch on the data structures where appropriate.
97

108

119
== Quick Start
1210

13-
1411
The easiest, and recommended, way to use Cats is to add the following imports:
1512

1613
```scala mdoc:silent
@@ -35,7 +32,7 @@ import cats.data.*
3532

3633

3734
Let's now see how we work with Cats,
38-
using [`cats.Show`][cats.Show] as an example.
35+
using #href("http://typelevel.org/cats/api/cats/Show.html")[`cats.Show`] as an example.
3936

4037
`Show` is Cats' equivalent of
4138
the `Display` type class we defined in @sec:type-classes:display.
@@ -52,7 +49,7 @@ trait Show[A] {
5249
```
5350

5451
The easiest way to use `Show` is with the wildcard import above.
55-
However, we can also import `Show` directly from the [cats][cats.package] package:
52+
However, we can also import `Show` directly from the `cats` package if we want:
5653

5754
```scala mdoc:silent
5855
import cats.Show
@@ -80,19 +77,21 @@ In the case of `Show`, an extension method `show` is defined.
8077
```
8178

8279
If, for some reason, we wanted just the syntax for `show`,
83-
we could import [`cats.syntax.show`][cats.syntax.show].
80+
we could import `cats.syntax.show`.
8481

8582
```scala mdoc:silent
8683
import cats.syntax.show.* // for show
8784
```
8885

8986

9087
=== Defining Custom Instances
91-
<defining-custom-instances>
92-
88+
<sec:cats:defining-custom-instances>
9389

94-
We can define an instance of `Show`
95-
simply by implementing the trait for a given type:
90+
As we learned in @sec:type-classes,
91+
we can define an instance of `Show`
92+
by implementing a given instance of the trait
93+
for a specific type.
94+
Let's implement `Show` for `java.util.Date`.
9695

9796
```scala mdoc:silent
9897
import java.util.Date
@@ -101,6 +100,9 @@ given dateShow: Show[Date] with
101100
def show(date: Date): String =
102101
s"${date.getTime}ms since the epoch."
103102
```
103+
104+
It works as expected.
105+
104106
```scala mdoc
105107
new Date().show
106108
```
@@ -141,10 +143,9 @@ for constructing instances, either from scratch
141143
or by transforming existing instances for other types.
142144

143145

144-
==== Exercise: Cat Show
145-
146+
#exercise[Cat Show]
146147

147-
Re-implement the `Cat` application from @sec:type-classes:cat
148+
In this exercise we'll re-implement the `Cat` application from @sec:type-classes:cat
148149
using `Show` instead of `Display`.
149150

150151
Using this data type to represent a well-known type of furry animal:

src/pages/fps.typ

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@
129129
// Cats
130130
#include "cats/index.typ"
131131
#include "cats/equal.typ"
132+
#include "cats/conclusions.typ"
132133
// Monoid
133134
#include "monoids/index.typ"
134135
#include "monoids/cats.typ"

src/pages/parts/part2.typ

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#import "../stdlib.typ": part
1+
#import "../stdlib.typ": part, href
22

33
#part[Type Classes] <sec:part:type-classes>
44

@@ -10,6 +10,6 @@ and as conceptual models that can drive program design.
1010
In this part we'll be looking more at their use for day-to-day programming,
1111
while the case studies will focus on their role in design.
1212

13-
In @sec:cats we introduce the #link("https://typelevel.org/cats")[Cats] library.
13+
In @sec:cats we introduce the #href("https://typelevel.org/cats")[Cats] library.
1414
Cats provides implementation of the type classes we're interested in,
1515
and so it saves a lot of time and typing to use it.

0 commit comments

Comments
 (0)