Up and Running with the
Typelevel Stack
Scala Italy 2018
Cats & Cats-Effect
● Lightweight functional programming library
● Provides abstractions that help with pure FP
● Aims to make code highly composable and reusable
● One of the most important concept is “Programs as values”
Pure Functional Programming
● Code completely free of side-effects
● Effects are modelled as values to be executed by the runtime
● The IO data type is an example of such an effect type
● We can model our entire application as a single value of IO
Http4s
● Lightweight typeful, pure functional http library
● Makes it very easy to build classic server applications
● Purely functional, but also very performant
● Offers a DSL for easily matching HTTP routes
type Service = Request => IO[Response]
Http4s DSL - example
HttpService[IO] {
case GET -> Root / name =>
Ok(s“Hello $name”)
}
Http4s DSL - example
HttpService[IO] {
case GET -> Root / name =>
Ok(s“Hello $name”)
}
Request
Http4s DSL - example
HttpService[IO] {
case GET -> Root / name =>
Ok(s“Hello $name”)
}
IO[Response]
Http4s DSL - example
HttpService[IO] {
case GET -> Root / name =>
Ok(s“Hello $name”)
}
Method
Http4s DSL - example
HttpService[IO] {
case GET -> Root / name =>
Ok(s“Hello $name”)
}
Path
Http4s DSL - example
HttpService[IO] {
case GET -> Root / name =>
Ok(s“Hello $name”)
}
String
Circe
● Lightweight JSON library
● Built upon Cats and Shapeless
● Allows us to derive encoders and decoders for our custom
data types
● One of the most widely used JSON libraries in Scala
Doobie
● Functional abstraction over JDBC
● Allows us to represent database programs as values of
the ConnectionIO data type
● We can then turn values of ConnectionIO into
values of IO as a transaction.
● Automatic mapping of query results to case classes
Doobie - example
val allUsers: ConnectionIO[List[User]] =
sql"SELECT * FROM user".query[User].to[List]
val luka: ConnectionIO[Option[User]] =
sql"SELECT * FROM user WHERE $name = ‘Luka’"
.query[User].option
val program: IO[List[User]] = allUsers.transact(xa)
Let’s start!
Clone the workshop repo:
https://github.com/LukaJCB/scala-italy-workshop
Documentation and assignments are in the README.md

Up and Running with the Typelevel Stack

  • 1.
    Up and Runningwith the Typelevel Stack Scala Italy 2018
  • 2.
    Cats & Cats-Effect ●Lightweight functional programming library ● Provides abstractions that help with pure FP ● Aims to make code highly composable and reusable ● One of the most important concept is “Programs as values”
  • 3.
    Pure Functional Programming ●Code completely free of side-effects ● Effects are modelled as values to be executed by the runtime ● The IO data type is an example of such an effect type ● We can model our entire application as a single value of IO
  • 4.
    Http4s ● Lightweight typeful,pure functional http library ● Makes it very easy to build classic server applications ● Purely functional, but also very performant ● Offers a DSL for easily matching HTTP routes
  • 5.
    type Service =Request => IO[Response]
  • 6.
    Http4s DSL -example HttpService[IO] { case GET -> Root / name => Ok(s“Hello $name”) }
  • 7.
    Http4s DSL -example HttpService[IO] { case GET -> Root / name => Ok(s“Hello $name”) } Request
  • 8.
    Http4s DSL -example HttpService[IO] { case GET -> Root / name => Ok(s“Hello $name”) } IO[Response]
  • 9.
    Http4s DSL -example HttpService[IO] { case GET -> Root / name => Ok(s“Hello $name”) } Method
  • 10.
    Http4s DSL -example HttpService[IO] { case GET -> Root / name => Ok(s“Hello $name”) } Path
  • 11.
    Http4s DSL -example HttpService[IO] { case GET -> Root / name => Ok(s“Hello $name”) } String
  • 12.
    Circe ● Lightweight JSONlibrary ● Built upon Cats and Shapeless ● Allows us to derive encoders and decoders for our custom data types ● One of the most widely used JSON libraries in Scala
  • 13.
    Doobie ● Functional abstractionover JDBC ● Allows us to represent database programs as values of the ConnectionIO data type ● We can then turn values of ConnectionIO into values of IO as a transaction. ● Automatic mapping of query results to case classes
  • 14.
    Doobie - example valallUsers: ConnectionIO[List[User]] = sql"SELECT * FROM user".query[User].to[List] val luka: ConnectionIO[Option[User]] = sql"SELECT * FROM user WHERE $name = ‘Luka’" .query[User].option val program: IO[List[User]] = allUsers.transact(xa)
  • 15.
    Let’s start! Clone theworkshop repo: https://github.com/LukaJCB/scala-italy-workshop Documentation and assignments are in the README.md