Having fun with Cats

Cats is a library for Functional Programming in Scala. The name come from the Inspiration by the Category Theory so Cats is short for Category Theory. Cats are about types, Type System and having abstractions to make it easy to work with them. If you want to go to the next level of function programming Cats is the way to go, however, I need to say that it's hard, get the ideas to take time, understand the terms, principles, and applications takes times as well. At the end of the day, this is about style so there is no right or wrong. Recently I made a post about Monads so it might be useful for you to read it if you did not read it yet. This post won't be monad heavy but reading about monads will make it easy to understand some things here. It's fine if you don't understand everything, I'm 100% sure I don't understand everything and there are lots of ideas and concepts that I still learning. So this takes time. I don't think this is something will make sense right of way or you will be able to apply the right of way unless you already studying this or is a Haskell Engineer. For me was fun to learn Cats(I'm still learning) but you might find not cool at all which is fine too. I work with multiples languages like Java, C, Scala, and Python. I have more fun with Scala rather than other languages, however, using Scala and Cats: The compiler errors and are the most complicated and hard to figure it out what I'm doing it wrong. Sometimes it feels like I'm trying to solve a puzzle that I have no idea what I'm doing. So I don't think math makes programming 100% more clean than OOP and sometimes to understand my uncle abstractions are faster, easier and less painful but they are not math proofed since is my uncle abstractions :-) The disclaimer is, it has some middle ground between theory and practical programming.

The Cats Structure

Cats always have this Structure:

  • A Type Class: Which is a trait in Scala
  • A Type Instance: Which is an Object in Scala
  • A Type Interface: Which is an Object in Scala
  • An Implicit Class: Which use an implicit class in Scala for better syntax
This way there is complete decoupling from the Alberic Type(Type Class) from Type Instance which is the binding for the Types you want to support and Type Interface which is the functions that will be generic for all your supported types. It's possible to add more Types to this system without having to touch previous classes that why type classes are much better for extension than OOP.  Let's take a look into a code sample to get this pattern right. 

Here we have all patterns together in the same code sample. The type class here is called Show, the idea of the show is to provide contract on how to Show some type, which might be printing to the console or logging into a file or send to a TCP port for the centralized log, it does not mapper function is decoupled from Type here. Them we have our Type Instances, there are 2 type instances: for Int and for String, this means we cant show Double for instance, If you want show Double you can provide the Type Implementation and that happens outside of this file so as you can see this is super extensive, correct and backed by the compiler. Them we can the Type interface which is the ShowInterface that for this case is how we will provide functionality for the Type. Finally, we have an implicit class in order to provide better syntactical sugar. 

A Bit of Theory and Definitions

Cats have Type Classes like SemiGroup, Functor, Monoid, Applicative, and many others and also has Data Types like Either, Eval, Ior, Id, Kleisli, State, and many many others. Data Types inherit from each other and this will the pileup functions. Pretty much all start with a SemiGroup which is just a Type that implements combine and it's capable to combine 2 Types of the Same Type. So Monoid is just a SemiGroup that provides the empty function. Cats provide type instances for most common and useful type in Scala such as Lists, Maps, Option, Either, Try, Future and many many others. So you don't need to define the algebra and functions for these types since Cats provide that for you out of the box, however, you might need provide evidence(implementation) for your Own business types. Functor is something that provides map implementation and is great to apply effects to data. There are type classes instances for Functors, Monoids, and Monads from the same common types I mentioned before. Type Classes have laws which need to be true always, Laws are something that gives your guarantee of composability however they don't help you to understand what the type is or what the type is doing. Some types in Cats are very straightforward and easy to understand like SemiGroup and other are very complicated like Cons.

Scala and Cats

Let's see how easy(for this samples :-)) is for you to use Cats with Scala.

Monoid Sample - Here we are using Option and monoid and combining 2 options.


Either Sample - Here we are using Int as Either with nice syntax and combining them with For Compreenshions in Scala.


Functor Sample - Here we have Functor Lifting where we have a normal function and turn into a Functor without having to implement type algebra.



That's it - I hope you had fun and that trigger your curiosity to Learn more about Cats and Function Programming Patterns, Math and Decoupling. I'm not an FP Zealot, however, I see the benefit in Theory so I search found the balance between Theory and practical programming. If you might wondering, does anyone use this kind of ideas in production at Scala? Yes, Twitter, Yelp, Verizon and many other big companies, These ideas used in Spark and Big Data for instance as well.

Cheers,
Diego Pacheco

Popular posts from this blog

Kafka Streams with Java 15

Rust and Java Interoperability

HMAC in Java