Skip to content

Commit c701416

Browse files
author
Abhijit Sarkar
committed
Complete ch06
1 parent 7d88efe commit c701416

File tree

5 files changed

+63
-1
lines changed

5 files changed

+63
-1
lines changed

.scalafmt.conf

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@ runner.dialect = scala3
55
assumeStandardLibraryStripMargin = true
66
# https://github.com/scalameta/scalameta/issues/4090
77
project.excludePaths = [
8-
"glob:**/ch04/src/**.scala"
8+
"glob:**/ch04/src/**.scala",
9+
"glob:**/ch06/src/Cat.scala"
910
]

build.mill

+4
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ trait CatsModule extends ScalaModule with Cross.Module[String] with ScalafmtModu
3333
"-source", "future",
3434
)
3535

36+
def ivyDeps = Agg(
37+
ivy"org.typelevel::cats-core:${v.catsVersion}"
38+
)
39+
3640
object test extends ScalaTests with TestModule.ScalaTest {
3741
val commonDeps = Seq(
3842
ivy"org.scalatest::scalatest:${v.scalatestVersion}",

ch06/src/Cat.scala

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package ch06
2+
3+
import cats.Show
4+
import cats.instances.int.catsStdShowForInt
5+
import cats.instances.string.catsStdShowForString
6+
import cats.syntax.show.toShow
7+
import cats.Eq
8+
import cats.syntax.eq.catsSyntaxEq
9+
10+
final case class Cat(name: String, age: Int, color: String)
11+
12+
object Cat:
13+
/*
14+
6.2.1.1 Exercise: Cat Show
15+
Re-implement the Cat application from Section 4.5.1 using Show instead of Display.
16+
*/
17+
given Show[Cat]:
18+
override def show(cat: Cat): String =
19+
val name = cat.name.show
20+
val age = cat.age.show
21+
val color = cat.color.show
22+
s"$name is a $age year-old $color cat."
23+
24+
/*
25+
6.3.4.1 Exercise: Equality, Liberty, and Felinity
26+
Implement an instance of Eq for our running Cat example.
27+
*/
28+
given Eq[Cat]:
29+
override def eqv(x: Cat, y: Cat): Boolean =
30+
(x.name === y.name) &&
31+
(x.age === y.age) &&
32+
(x.color === y.color)

ch06/test/src/CatSpec.scala

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package ch06
2+
3+
import org.scalatest.funspec.AnyFunSpec
4+
import org.scalatest.matchers.should.Matchers.shouldBe
5+
import cats.syntax.show.toShow
6+
import cats.syntax.eq.catsSyntaxEq
7+
8+
class CatSpec extends AnyFunSpec:
9+
describe("Cat"):
10+
it("Show"):
11+
Cat("Garfield", 41, "ginger and black").show `shouldBe` "Garfield is a 41 year-old ginger and black cat."
12+
13+
it("Eq"):
14+
val cat1 = Cat("Garfield", 38, "orange and black")
15+
val cat2 = Cat("Heathcliff", 32, "orange and black")
16+
17+
cat1 === cat2 `shouldBe` false
18+
cat1 =!= cat2 `shouldBe` true
19+
20+
val optionCat1 = Option(cat1)
21+
val optionCat2 = Option.empty[Cat]
22+
23+
optionCat1 === optionCat2 `shouldBe` false
24+
optionCat1 =!= optionCat2 `shouldBe` true

versions.mill

+1
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ package build
33
val scalaVersion = "3.6.2"
44
val scalatestVersion = "3.2.19"
55
val scalacheckVersion = "3.2.19.0"
6+
val catsVersion = "2.12.0"

0 commit comments

Comments
 (0)