Commit c701416 Abhijit Sarkar
committed
1 parent 7d88efe commit c701416 Copy full SHA for c701416
File tree 5 files changed +63
-1
lines changed
5 files changed +63
-1
lines changed Original file line number Diff line number Diff line change @@ -5,5 +5,6 @@ runner.dialect = scala3
5
5
assumeStandardLibraryStripMargin = true
6
6
# https://github.com/scalameta/scalameta/issues/4090
7
7
project.excludePaths = [
8
- "glob:**/ch04/src/**.scala"
8
+ "glob:**/ch04/src/**.scala" ,
9
+ "glob:**/ch06/src/Cat.scala"
9
10
]
Original file line number Diff line number Diff line change @@ -33,6 +33,10 @@ trait CatsModule extends ScalaModule with Cross.Module[String] with ScalafmtModu
33
33
"-source", "future",
34
34
)
35
35
36
+ def ivyDeps = Agg(
37
+ ivy"org.typelevel::cats-core:${v.catsVersion}"
38
+ )
39
+
36
40
object test extends ScalaTests with TestModule.ScalaTest {
37
41
val commonDeps = Seq(
38
42
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
3
3
val scalaVersion = "3.6.2"
4
4
val scalatestVersion = "3.2.19"
5
5
val scalacheckVersion = "3.2.19.0"
6
+ val catsVersion = "2.12.0"
You can’t perform that action at this time.
0 commit comments