-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Abhijit Sarkar
committed
Dec 22, 2024
1 parent
7d88efe
commit c701416
Showing
5 changed files
with
63 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters