File tree Expand file tree Collapse file tree 6 files changed +64
-1
lines changed Expand file tree Collapse file tree 6 files changed +64
-1
lines changed Original file line number Diff line number Diff line change @@ -5,5 +5,6 @@ runner.dialect = scala3
55assumeStandardLibraryStripMargin = true
66# https://github.com/scalameta/scalameta/issues/4090
77project.excludePaths = [
8- "glob:**/ch04/src/**.scala"
8+ "glob:**/ch04/src/**.scala" ,
9+ "glob:**/ch06/src/Cat.scala"
910 ]
Original file line number Diff line number Diff line change @@ -13,6 +13,7 @@ The older code is available in branches.
13133 . [ Objects as Codata] ( ch03 )
14144 . [ Contextual Abstraction] ( ch04 )
15155 . [ Reified Interpreters] ( ch05 )
16+ 6 . [ Using Cats] ( ch06 )
1617
1718## Running tests
1819```
Original file line number Diff line number Diff 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}",
Original file line number Diff line number Diff line change 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)
Original file line number Diff line number Diff line change 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
Original file line number Diff line number Diff line change @@ -3,3 +3,4 @@ package build
33val scalaVersion = "3.6.2"
44val scalatestVersion = "3.2.19"
55val scalacheckVersion = "3.2.19.0"
6+ val catsVersion = "2.12.0"
You can’t perform that action at this time.
0 commit comments