Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Cats in Scala

Scala is one of the most commercially successful programming languages in which functional programming is not only possible, but it is actively supported by language features, popular libraries, and many prominent advocates of functional programming. 

One of the popular Library is Cats Library. It provides abstractions for functional programming in the Scala programming language.

  • Login to see the comments

  • Be the first to like this

Cats in Scala

  1. 1. Prabhat Kashyap Sr. Software Consultant Knoldus Inc. Cats in Scala Presented By: Prabhat Kashyap Sr. Software Consultant
  2. 2. Agenda  Introduction  Type class  Monoids and Semigroups  Functors  Monads  Monad Transformers
  3. 3. 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
  4. 4. Type Class
  5. 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. 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. 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. 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. 9. 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
  10. 10. 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.
  11. 11. 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
  12. 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. 13. 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 }
  14. 14. 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")
  15. 15. 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)
  16. 16. 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)
  17. 17. 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))
  18. 18. Functors in Cats Syntax ●The funtor type class is cats.syntax.functor
  19. 19. 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?
  20. 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. 21. 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)
  22. 22. 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
  23. 23. 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 }
  24. 24. 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
  25. 25. Reference
  26. 26. Any queriesor Feedback? Email @ prabhat.kashyap@knoldus.in

×