Presented By: Mansi Babbar
Getting Started with
Scala Cats
Lack of etiquette and manners is a huge turn off.
KnolX Etiquettes
Punctuality
Respect Knolx session timings, you
are requested not to join sessions
after a 5 minutes threshold post
the session start time.
Feedback
Make sure to submit a constructive
feedback for all sessions as it is
very helpful for the presenter.
Mute
Please keep your window on mute
Avoid Disturbance
Avoid leaving your window
unmuted after asking a question
Agenda
What is Cats?
03 What are Implicits?
04 Implementation - The Cat Show
What are Type Classes?02
01
Meet Cats
● An abstraction for functional programming in
the Scala programming language.
● Cats contain a large variety of tools for
functional programming majorly in form of
type classes which we can apply to the
existing Scala types.
What is Cats?
Type Classes
What are Type Classes?
● Type classes are a pattern in
programming originating in Haskell.
● Allow us to extend existing libraries
with new functionality, without using
traditional inheritance, and also without
altering the original library source code.
Type Class Components
Type Class :
● An API or interface that represents some functionality.
● Represented by a trait with at least one type parameter.
Type Class Components
Type Class Instances :
● Provide implementations of the type class for specific types.
● In Scala, we define instances by creating concrete implementations of the
type class and tagging them with the implicit keyword:
Type Class Components
Type Class Use :
● Any functionality which requires a type class instance to work.
● In Scala, this means any method that accepts instances of the type class as
implicit parameters.
● Cats provide utilities which make type classes a little bit easier to use, and
you may sometimes see these patterns in other libraries also.
● Type class can be used by creating Interface Objects.
Type Class Components
Interface Objects :
The simplest way of creating an interface that uses a type class is to place
methods in a singleton object:
Type Class Components
Interface Objects :
To use this object, we have to import any type class instances we care about
and then call the relevant method:
Type Class Components
Implicits
What are Implicits?
● Working with implicit values and the implicit
parameters.
● Placing the instances in a companion object to
the type class has a very special significance in
Scala because it actually plays into something
called implicit scope.
What is Implicit Scope?
● Compiler will search for the candidate type class instances by type.
● Places where the compiler will search for the candidate instances is known as the
implicit scope.
● It applies at the call site which is the point where we call a method with an
implicit parameter.
● Definitions will only be included in implicit scope if they are tagged with the
implicit keyword.
● Compiler will fail with an ambiguous implicit values error if it sees multiple
candidate definitions.
What is Implicit Scope?
For example, in the following expression it will look for an instance of type
JsonWriter[String]:
What is Implicit Scope?
Type class instances packaging can be done as:
● by placing them in an object such as JsonWriterInstances
● by placing them in a trait
● by placing them in the companion object of the type class
Demo: The Cat Show
References
● https://typelevel.org/cats/
● http://underscore.io/books/scala-with-cats/
● https://www.scalawithcats.com/dist/scala-with-
cats.html
● https://github.com/typelevel/cats
● https://medium.com/@terezk_a/haskell-in-elm-
terms-type-classes-415f1612b335
Thank You :)

Getting started with scala cats

  • 1.
    Presented By: MansiBabbar Getting Started with Scala Cats
  • 2.
    Lack of etiquetteand manners is a huge turn off. KnolX Etiquettes Punctuality Respect Knolx session timings, you are requested not to join sessions after a 5 minutes threshold post the session start time. Feedback Make sure to submit a constructive feedback for all sessions as it is very helpful for the presenter. Mute Please keep your window on mute Avoid Disturbance Avoid leaving your window unmuted after asking a question
  • 3.
    Agenda What is Cats? 03What are Implicits? 04 Implementation - The Cat Show What are Type Classes?02 01
  • 4.
  • 5.
    ● An abstractionfor functional programming in the Scala programming language. ● Cats contain a large variety of tools for functional programming majorly in form of type classes which we can apply to the existing Scala types. What is Cats?
  • 6.
  • 7.
    What are TypeClasses? ● Type classes are a pattern in programming originating in Haskell. ● Allow us to extend existing libraries with new functionality, without using traditional inheritance, and also without altering the original library source code.
  • 8.
  • 9.
    Type Class : ●An API or interface that represents some functionality. ● Represented by a trait with at least one type parameter. Type Class Components
  • 10.
    Type Class Instances: ● Provide implementations of the type class for specific types. ● In Scala, we define instances by creating concrete implementations of the type class and tagging them with the implicit keyword: Type Class Components
  • 11.
    Type Class Use: ● Any functionality which requires a type class instance to work. ● In Scala, this means any method that accepts instances of the type class as implicit parameters. ● Cats provide utilities which make type classes a little bit easier to use, and you may sometimes see these patterns in other libraries also. ● Type class can be used by creating Interface Objects. Type Class Components
  • 12.
    Interface Objects : Thesimplest way of creating an interface that uses a type class is to place methods in a singleton object: Type Class Components
  • 13.
    Interface Objects : Touse this object, we have to import any type class instances we care about and then call the relevant method: Type Class Components
  • 14.
  • 15.
    What are Implicits? ●Working with implicit values and the implicit parameters. ● Placing the instances in a companion object to the type class has a very special significance in Scala because it actually plays into something called implicit scope.
  • 16.
    What is ImplicitScope? ● Compiler will search for the candidate type class instances by type. ● Places where the compiler will search for the candidate instances is known as the implicit scope. ● It applies at the call site which is the point where we call a method with an implicit parameter. ● Definitions will only be included in implicit scope if they are tagged with the implicit keyword. ● Compiler will fail with an ambiguous implicit values error if it sees multiple candidate definitions.
  • 17.
    What is ImplicitScope? For example, in the following expression it will look for an instance of type JsonWriter[String]:
  • 18.
    What is ImplicitScope? Type class instances packaging can be done as: ● by placing them in an object such as JsonWriterInstances ● by placing them in a trait ● by placing them in the companion object of the type class
  • 19.
  • 20.
    References ● https://typelevel.org/cats/ ● http://underscore.io/books/scala-with-cats/ ●https://www.scalawithcats.com/dist/scala-with- cats.html ● https://github.com/typelevel/cats ● https://medium.com/@terezk_a/haskell-in-elm- terms-type-classes-415f1612b335
  • 21.