Prabhat Kashyap
Sr. Software Consultant
Knoldus Inc.
Cats in Scala
Presented By: Prabhat Kashyap
Sr. Software Consultant
Agenda
 Introduction
 Type class
 Monoids and Semigroups
 Functors
 Monads
 Monad Transformers
Introduction
Cats is a library which provides abstractions for functional
programming in the Scala programming language.
The name is a playful shortening of the word category.
-Documentation
Type Class
The Type Class
 Type classes were first introduced in Haskell as a new approach to ad-hoc polymorphism.
 Type classes in Haskell are based on Hindley–Milner type system.
 Scala's type inference is based on Local Type Inference.
object Math {
trait NumberLike[A] {
def add(a: A, b: A): A
def subtract(a: A, b: A): A
def multiply(a: A, b: A): A
def divide(a: A, b: A): A
}
}
Type Class Instances
 The instances of a type class provide implementations for the types.
 Members of type classes are usually singleton objects.
 implicit keyword before each of the type class implementations.
Type Class Instances [Example]
object Math {
trait NumberLike[A] {
def add(a: A, b: A): A
def subtract(a: A, b: A): A
def multiply(a: A, b: A): A
def divide(a: A, b: A): A
}
implicit val intLike: NumberLike[Int] = new
NumberLike[Int] {
override def add(a: Int, b: Int): Int = a + b
override def subtract(a: Int, b: Int): Int = a - b
override def multiply(a: Int, b: Int): Int = a * b
override def divide(a: Int, b: Int): Int = a / b
}
}
Type Class Interfaces
 A type class interface is any functionality we expose to users.
object DoTheMath{
import Math.NumberLike
def addition[A](a: A, b: A)(implicit ev: NumberLike[A]) =
ev.add(a, b)
}
DoTheMath.addition(1,2)
Type class in Cats
 Cats is written using a modular structure.
 It allows us to choose which type classes, instances, and interface methods we want to use.
import cats.Show
import cats.instances.int._
val showInt: Show[Int] = Show.apply[Int]
val intToString: String = showInt.show(1)
import cats.syntax.show._
123.show
Defining Custom Instances
import java.util.Date
implicit val dateShow: Show[Date] =
Show.show(date => s"${date.getTime}ms since the epoch.")
dateShow.show(new Date())
Result:
res2: String = 1532873812098ms since the epoch.
trait Monoid[A] {
def combine(x: A, y: A): A
def empty: A
}
A monoid for a type A is:
 an operation on combine with type (A, A) => A
 an element empty of type A
 Must follow associative law.
 Must follow identity law.
Monoids
Monoids [Few Example]
// Example Addition
1 + 1 = 2
1 + 0 = 1 & 0 + 1 = 1
1 + (1 + 2) = 4
(1 + 1) + 2 = 4
 Integer Addition
 Integer Multiplication
 String Concatatination
// Example Concatenation
“1” + “1” = “11”
“1” + “” = “1” & “” + “1” = “1”
“1” + (“1” + “2”) = “112”
(“1” + “1”) + “2” = “112”
// Example Subtraction
(4 – 2) – 2 = 0
4 – (2 – 2) = 4
Semigroup
trait Semigroup[A] {
def combine(x: A, y: A): A
}
 A semigroup is just the combine part of a monoid
 For example NonEmptyList or Positive Integer
trait Monoid[A] extends Semigroup[A] {
def empty: A
}
Monoids and Semigroup in Cats
Type Class
 The monoid type class is cats.kernel.Monoid , which is aliased as cats.Monoid.
 Monoid extends cats.kernel.Semigroup , which is aliased as cats.Semigroup.
Instance
import cats.Monoid
import cats.instances.string._
Monoid[String].combine("Hello ", " World")
Monoids and Semigroup in Cats
Syntax
●Cats provides syntax for the combine method (|+| operator).
import cats.instances.option._
import cats.syntax.semigroup._
val result = Option(1) |+| Monoid[Option[Int]].empty
Result = Option(1)
Functors
 A Functor is anything with a map method.
 For example: Option , List , and Either
 Functor as used to transforming the values inside. For example:
Option(1).map(_ + 1) // Option(2)
Result = Option(2)
Functors in Cats
Type Class
●The funtor type class is cats.Functor.
import cats.Functor
Functor[Option].map(Option(1))(_ + 2)
Instance
import cats.Functor
import cats.instances.option._
val optionIncrementFunc = Functor[Option].lift((x:Int) => x + 1)
val result = optionIncrementFunc(Option(2))
Functors in Cats
Syntax
●The funtor type class is cats.syntax.functor
Monads
 Monads are one of the most common abstractions in Scala.
 A monad is anything with a constructor and a flatMap method.
 All functors are monads.
A monad is a mechanism for sequencing computations.
Isn’t the functors are same as monads?
Monads Definition
trait Monad[F[_]] {
def pure[A](value: A): F[A]
def flatMap[A, B](value: F[A])(func: A => F[B]): F[B]
}
Monads in Cats
Type Class
The monad type class is cats.Monad.
import cats.Monad
Monad[Option].pure(3)
Instance
import cats.Monad
import cats.instances.option._
Monad[Option].flatMap(Option(3))(a => Some(a + 2))
// Option[Int] = Some(5)
Monads in Cats
Syntax
●cats.syntax.flatMap provides syntax for flatMap
●cats.syntax.functor provides syntax for map
●cats.syntax.applicative provides syntax for pure
Monads Transformation
 Monad transformation in standard Scala library is difficult and messy.
 Nested for-comprehension.
for {
optUser <- listOfUser
} yield {
for {
user <- optUser
} yield user.name
}
Cat Transformers
 Cats provides transformers for many monads, each named with a T suffix.
 EitherT composes Either with other monads, OptionT composes Option , and so on.
 Each monad transformer is a data type, defined in cats.data.
cats.data.OptionT
cats.data.EitherT
cats.data.ReaderT
cats.data.WriterT
cats.data.StateT
cats.data.IdT
Reference
Any queriesor Feedback?
Email @ prabhat.kashyap@knoldus.in
Cats in Scala

Cats in Scala

  • 1.
    Prabhat Kashyap Sr. SoftwareConsultant Knoldus Inc. Cats in Scala Presented By: Prabhat Kashyap Sr. Software Consultant
  • 2.
    Agenda  Introduction  Typeclass  Monoids and Semigroups  Functors  Monads  Monad Transformers
  • 3.
    Introduction Cats is alibrary which provides abstractions for functional programming in the Scala programming language. The name is a playful shortening of the word category. -Documentation
  • 4.
  • 5.
    The Type Class Type classes were first introduced in Haskell as a new approach to ad-hoc polymorphism.  Type classes in Haskell are based on Hindley–Milner type system.  Scala's type inference is based on Local Type Inference. object Math { trait NumberLike[A] { def add(a: A, b: A): A def subtract(a: A, b: A): A def multiply(a: A, b: A): A def divide(a: A, b: A): A } }
  • 6.
    Type Class Instances The instances of a type class provide implementations for the types.  Members of type classes are usually singleton objects.  implicit keyword before each of the type class implementations.
  • 7.
    Type Class Instances[Example] object Math { trait NumberLike[A] { def add(a: A, b: A): A def subtract(a: A, b: A): A def multiply(a: A, b: A): A def divide(a: A, b: A): A } implicit val intLike: NumberLike[Int] = new NumberLike[Int] { override def add(a: Int, b: Int): Int = a + b override def subtract(a: Int, b: Int): Int = a - b override def multiply(a: Int, b: Int): Int = a * b override def divide(a: Int, b: Int): Int = a / b } }
  • 8.
    Type Class Interfaces A type class interface is any functionality we expose to users. object DoTheMath{ import Math.NumberLike def addition[A](a: A, b: A)(implicit ev: NumberLike[A]) = ev.add(a, b) } DoTheMath.addition(1,2)
  • 9.
    Type class inCats  Cats is written using a modular structure.  It allows us to choose which type classes, instances, and interface methods we want to use. import cats.Show import cats.instances.int._ val showInt: Show[Int] = Show.apply[Int] val intToString: String = showInt.show(1) import cats.syntax.show._ 123.show
  • 10.
    Defining Custom Instances importjava.util.Date implicit val dateShow: Show[Date] = Show.show(date => s"${date.getTime}ms since the epoch.") dateShow.show(new Date()) Result: res2: String = 1532873812098ms since the epoch.
  • 11.
    trait Monoid[A] { defcombine(x: A, y: A): A def empty: A } A monoid for a type A is:  an operation on combine with type (A, A) => A  an element empty of type A  Must follow associative law.  Must follow identity law. Monoids
  • 12.
    Monoids [Few Example] //Example Addition 1 + 1 = 2 1 + 0 = 1 & 0 + 1 = 1 1 + (1 + 2) = 4 (1 + 1) + 2 = 4  Integer Addition  Integer Multiplication  String Concatatination // Example Concatenation “1” + “1” = “11” “1” + “” = “1” & “” + “1” = “1” “1” + (“1” + “2”) = “112” (“1” + “1”) + “2” = “112” // Example Subtraction (4 – 2) – 2 = 0 4 – (2 – 2) = 4
  • 13.
    Semigroup trait Semigroup[A] { defcombine(x: A, y: A): A }  A semigroup is just the combine part of a monoid  For example NonEmptyList or Positive Integer trait Monoid[A] extends Semigroup[A] { def empty: A }
  • 14.
    Monoids and Semigroupin Cats Type Class  The monoid type class is cats.kernel.Monoid , which is aliased as cats.Monoid.  Monoid extends cats.kernel.Semigroup , which is aliased as cats.Semigroup. Instance import cats.Monoid import cats.instances.string._ Monoid[String].combine("Hello ", " World")
  • 15.
    Monoids and Semigroupin Cats Syntax ●Cats provides syntax for the combine method (|+| operator). import cats.instances.option._ import cats.syntax.semigroup._ val result = Option(1) |+| Monoid[Option[Int]].empty Result = Option(1)
  • 16.
    Functors  A Functoris anything with a map method.  For example: Option , List , and Either  Functor as used to transforming the values inside. For example: Option(1).map(_ + 1) // Option(2) Result = Option(2)
  • 17.
    Functors in Cats TypeClass ●The funtor type class is cats.Functor. import cats.Functor Functor[Option].map(Option(1))(_ + 2) Instance import cats.Functor import cats.instances.option._ val optionIncrementFunc = Functor[Option].lift((x:Int) => x + 1) val result = optionIncrementFunc(Option(2))
  • 18.
    Functors in Cats Syntax ●Thefuntor type class is cats.syntax.functor
  • 19.
    Monads  Monads areone of the most common abstractions in Scala.  A monad is anything with a constructor and a flatMap method.  All functors are monads. A monad is a mechanism for sequencing computations. Isn’t the functors are same as monads?
  • 20.
    Monads Definition trait Monad[F[_]]{ def pure[A](value: A): F[A] def flatMap[A, B](value: F[A])(func: A => F[B]): F[B] }
  • 21.
    Monads in Cats TypeClass The monad type class is cats.Monad. import cats.Monad Monad[Option].pure(3) Instance import cats.Monad import cats.instances.option._ Monad[Option].flatMap(Option(3))(a => Some(a + 2)) // Option[Int] = Some(5)
  • 22.
    Monads in Cats Syntax ●cats.syntax.flatMapprovides syntax for flatMap ●cats.syntax.functor provides syntax for map ●cats.syntax.applicative provides syntax for pure
  • 23.
    Monads Transformation  Monadtransformation in standard Scala library is difficult and messy.  Nested for-comprehension. for { optUser <- listOfUser } yield { for { user <- optUser } yield user.name }
  • 24.
    Cat Transformers  Catsprovides transformers for many monads, each named with a T suffix.  EitherT composes Either with other monads, OptionT composes Option , and so on.  Each monad transformer is a data type, defined in cats.data. cats.data.OptionT cats.data.EitherT cats.data.ReaderT cats.data.WriterT cats.data.StateT cats.data.IdT
  • 25.
  • 26.
    Any queriesor Feedback? Email@ prabhat.kashyap@knoldus.in