Skip to content

Commit

Permalink
Complete ch06
Browse files Browse the repository at this point in the history
  • Loading branch information
Abhijit Sarkar committed Dec 22, 2024
1 parent 7d88efe commit ce1f067
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 1 deletion.
3 changes: 2 additions & 1 deletion .scalafmt.conf
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ runner.dialect = scala3
assumeStandardLibraryStripMargin = true
# https://github.com/scalameta/scalameta/issues/4090
project.excludePaths = [
"glob:**/ch04/src/**.scala"
"glob:**/ch04/src/**.scala",
"glob:**/ch06/src/Cat.scala"
]
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ The older code is available in branches.
3. [Objects as Codata](ch03)
4. [Contextual Abstraction](ch04)
5. [Reified Interpreters](ch05)
6. [Using Cats](ch06)

## Running tests
```
Expand Down
4 changes: 4 additions & 0 deletions build.mill
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ trait CatsModule extends ScalaModule with Cross.Module[String] with ScalafmtModu
"-source", "future",
)

def ivyDeps = Agg(
ivy"org.typelevel::cats-core:${v.catsVersion}"
)

object test extends ScalaTests with TestModule.ScalaTest {
val commonDeps = Seq(
ivy"org.scalatest::scalatest:${v.scalatestVersion}",
Expand Down
32 changes: 32 additions & 0 deletions ch06/src/Cat.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package ch06

import cats.Show
import cats.instances.int.catsStdShowForInt
import cats.instances.string.catsStdShowForString
import cats.syntax.show.toShow
import cats.Eq
import cats.syntax.eq.catsSyntaxEq

final case class Cat(name: String, age: Int, color: String)

object Cat:
/*
6.2.1.1 Exercise: Cat Show
Re-implement the Cat application from Section 4.5.1 using Show instead of Display.
*/
given Show[Cat]:
override def show(cat: Cat): String =
val name = cat.name.show
val age = cat.age.show
val color = cat.color.show
s"$name is a $age year-old $color cat."

/*
6.3.4.1 Exercise: Equality, Liberty, and Felinity
Implement an instance of Eq for our running Cat example.
*/
given Eq[Cat]:
override def eqv(x: Cat, y: Cat): Boolean =
(x.name === y.name) &&
(x.age === y.age) &&
(x.color === y.color)
24 changes: 24 additions & 0 deletions ch06/test/src/CatSpec.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package ch06

import org.scalatest.funspec.AnyFunSpec
import org.scalatest.matchers.should.Matchers.shouldBe
import cats.syntax.show.toShow
import cats.syntax.eq.catsSyntaxEq

class CatSpec extends AnyFunSpec:
describe("Cat"):
it("Show"):
Cat("Garfield", 41, "ginger and black").show `shouldBe` "Garfield is a 41 year-old ginger and black cat."

it("Eq"):
val cat1 = Cat("Garfield", 38, "orange and black")
val cat2 = Cat("Heathcliff", 32, "orange and black")

cat1 === cat2 `shouldBe` false
cat1 =!= cat2 `shouldBe` true

val optionCat1 = Option(cat1)
val optionCat2 = Option.empty[Cat]

optionCat1 === optionCat2 `shouldBe` false
optionCat1 =!= optionCat2 `shouldBe` true
1 change: 1 addition & 0 deletions versions.mill
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ package build
val scalaVersion = "3.6.2"
val scalatestVersion = "3.2.19"
val scalacheckVersion = "3.2.19.0"
val catsVersion = "2.12.0"

0 comments on commit ce1f067

Please sign in to comment.