Scala Cats to handle side effects for writing purely functional code..
Scala Cats came into picture later after Scalaz a Scala library for functional programming wasn't able to meet the some of the expectations. Scalaz which provides foundation for type classes like Functor, Monad etc. was started as an attempt to import some of the well established abstractions from popular functional languages like Haskell. But scalaz wasn't able to provide proper documentation for same. Hence Haskell documentation and resources were required at time to understand how to use certain functional programming libraries for type classes. Scalaz came to provide purely functional data structures to complement those from the Scala standard library.?
Scalaz Equals: Scala provides us a double equals operator (==) for comparing any two values for equality. But it?lacks type-safety. Like if we try to compare values of different types will always yield?false. Scalaz solves this problem by providing a?triple equals operator (===) to add the type-safety?that is needed. Using === for comparing values of different types will always yield in a compile-time failure:?
Type Mismatch.
Required: Int
Found: String
assertTrue(4 === 4)
Other example of scalaz type-classes includes: Order, Show, Enum, Option Operations, String Operations, Boolean Operations, Map Operations, NonEmptyList, Lens.
Now let's understand need for Scala Cats: Cats was developed later then essentially as re-implementation of Scalaz library. There are also libraries which serve as compatibility layer among both libraries. Scala Cats is a library which provides abstractions for functional programming in scala. The name for this library Cats was adopt from Category Theory same origin from where functional programming has roots too. So we can interpret the main task of cats as is to provide foundation for an ecosystem of pure type libraries to support functional programming in scala language. This library is written using modular approach which provides privilege to choose type of class, instance and interface required. Dependencies used for cats library:
libraryDependencies += "org.typelevel" %% "cats-core" % "x.x.x"
This cats library primarily depends on two things: one is type classes and other is implicits. Wherein Type Class is an interface/API that represents some functionality to implement. They can be split into three categories: type class, type class instance, type class interface. And scala implicits means values to be taken from context in which they are called. If there are no implicits of correct type in given scope/context it won't compile. Now implicit further can be different types: implicit parameter, implicit functions/type conversion, implicit class, implicit objects/type class.
Now let's discuss about another concept within cats that is Cats Effect which is a library/framework that allows to write composable and high performance applications with pure Functional Programming. Libraries like ZIO, Monix IO & cats effect attempt to provide an effect system for scala.
领英推荐
Let's understand need for Effect System: A function is set to effect free when it has no observable result besides its return type i.e. pure function. Popular functional languages like Haskell includes an effect system by default. But languages like Scala doesn't hasn't inbuilt effect system but its type system is strong enough to build an effect system library with only limitation that compiler won't be able to enforce its proper use.
Effect system via. cats effect 3 controls effects and maximise composability in code. There is a need to control effects as FP favors pure functions and composition. In real-time programs there must be an effect like printing to console, write to file etc. Since majority of code will contain a mix of pure and effectful code we need to control boundary between both types of code. This is wherein effect system plays role.
Cats Effect come with powerful tool that is IO Monad which gives power to compose any kind of actions i.e. effects and provide great support for concurrency, parallelisms and coordination. All this comes with strongly typed system. It also comes with group of abstractions that provides a clear distinction between different kinds of effects with intent to write more composable code.
Side-effects are inevitable in real world scenarios. But we would also like our code to be purely functional following principles of referential transparency which can be followed rigorously only in absence of side effects. So we need this concept of effect that bridges gap between writing purely functional code with side effects in a real world scenario.
An effect is a datatype with bunch of properties that includes a side effect/computation required in code. Our intent should be for these types of datatype to figure out what kind of effects would be produced. And separate construction of effect from effect execution.
Effect type includes Option, IO Monad etc. So in order to bridge difference between side-effects and functional programming we need this datatype effect. Effect is a datatype which represents a computation concept which is referential transparent.