Skip to content

Commit f40d19f

Browse files
author
vaddya
committed
add cats submodule
1 parent e219c8c commit f40d19f

File tree

7 files changed

+95
-1
lines changed

7 files changed

+95
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@
77
* `capstone` - [Functional Programming in Scala Capstone](https://www.coursera.org/learn/scala-capstone), [Certificate](https://www.coursera.org/account/accomplishments/certificate/FLQGR3W83U9W)
88
* `reactive` - [Programming Reactive Systems](https://www.edx.org/course/programming-reactive-systems)
99
* `effective` - [Effective Programming in Scala](https://www.coursera.org/learn/effective-scala)
10+
* `cats` - [Scala with Cats Book](https://underscore.io/books/scala-with-cats/)

build.sbt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ lazy val global = project
1414
spark,
1515
capstone,
1616
reactive,
17-
effective
17+
effective,
18+
cats
1819
)
1920

2021
lazy val principles = sparkProject("principles")
@@ -24,6 +25,7 @@ lazy val spark = sparkProject("spark")
2425
lazy val capstone = sparkProject("capstone")
2526
lazy val reactive = sparkProject("reactive")
2627
lazy val effective = sparkProject("effective")
28+
lazy val cats = sparkProject("cats")
2729

2830
def sparkProject(dir: String) = Project(dir, file(dir))
2931
.settings(

cats/build.sbt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
libraryDependencies ++= Seq(
2+
"org.typelevel" %% "cats-core" % "2.7.0"
3+
)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package com.vaddya.fpscala.cats.intro
2+
3+
final case class Cat(name: String, age: Int, color: String)
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.vaddya.fpscala.cats.intro
2+
3+
import cats.Eq
4+
import cats.syntax.eq.*
5+
6+
object Eq {
7+
implicit val catEq: Eq[Cat] = (cat1, cat2) =>
8+
cat1 == cat2
9+
}
10+
11+
@main def runEq(): Unit =
12+
import Eq.*
13+
14+
val cat1 = Option(Cat("Busya", 6, "Black"))
15+
val cat2 = Option.empty[Cat]
16+
17+
println(cat1 === cat1)
18+
println(cat1 =!= cat2)
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.vaddya.fpscala.cats.intro
2+
3+
trait Printable[A]:
4+
def format(value: A): String
5+
6+
object Printable:
7+
// Scala 2
8+
implicit val stringPrintable: Printable[String] = (value: String) => value
9+
10+
// Scala 3
11+
given intPrintable: Printable[Int] with
12+
override def format(value: Int): String = value.toString
13+
14+
object PrintableInstances:
15+
// Scala 2
16+
def format[A](value: A)(implicit printable: Printable[A]): String =
17+
printable.format(value)
18+
19+
// Scala 3
20+
def print[A](value: A)(using printable: Printable[A]): Unit =
21+
println(format(value))
22+
23+
object PrintableSyntax:
24+
// Scala 2
25+
implicit class PrintableOps[A](value: A):
26+
def format(implicit printable: Printable[A]): String =
27+
printable.format(value)
28+
29+
// Scala 3
30+
extension[A] (value: A)
31+
def print(using printable: Printable[A]): Unit =
32+
println(summon[Printable[A]].format(value))
33+
34+
@main def runPrintable(): Unit =
35+
import PrintableInstances.print as printing
36+
import PrintableSyntax.*
37+
38+
given Printable[Cat] with
39+
override def format(value: Cat): String =
40+
val name = PrintableInstances.format(value.name)
41+
val age = PrintableInstances.format(value.age)
42+
val color = PrintableInstances.format(value.color)
43+
44+
s"$name is a $age year-old $color cat."
45+
46+
val cat = Cat("Busya", 6, "Black")
47+
printing(cat)
48+
cat.print
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.vaddya.fpscala.cats.intro
2+
3+
import cats.Show
4+
import cats.implicits.toShow
5+
import cats.instances.all.*
6+
7+
object Show:
8+
implicit val catShow: Show[Cat] = (cat: Cat) =>
9+
val name = cat.name.show
10+
val age = cat.age.show
11+
val color = cat.color.show
12+
13+
s"$name is a $age year-old $color cat."
14+
15+
@main def runShow(): Unit =
16+
import Show.*
17+
18+
val cat = Cat("Busya", 6, "Black")
19+
println(cat.show)

0 commit comments

Comments
 (0)