P.S This post is for beginners, experienced developers can skip this 🙂
In this post, I will try my best to avoid using conventions so as not to confuse the beginner, I just want to solely pass the message across without confusing the reader with general conventions.
As a Scala developer, I had always heard about Typeclasses, and it seemed really hard to understand, but I’ll try to simplify it so much so other beginners can easily understand this great feature in Scala.
Before I define what TypeClasses are, let’s start with a small problem and hope that by trying to solve the problem will help you understand the concept better.
You may be like me and may have wondered at some point how on earth you could run your Scala or Java programs from SBT with arguments, when all I did when running my program was pass the run command to SBT. So how do I pass JVM parameters or compiler parameters? Well, all of that can be done in your build.sbt config file as shown below
Huge disclaimer, I’m not yet some FP expert who can boldly explain category theory and all them really fancy terms, but this article explains how I was able to build a pretty basic and functional Todo CRUD app using ZIO and Akka Http.
Granted, there are some functional Http clients such as http4s, but I’ve been using Akka Http for ages and I just needed some FP library that I could integrate with Akka Http, and since there is so much buzz around ZIO, I decided to use this pretty awesome library. So here goes 😄.
Before we get into some code, I just wanna explain what I think an effect is as this is one term that will be used a lot in this article. As easily explained in this blogpost, an <strong>effect</strong> is what a Monad models, or handles, when you see an Option, you immediately think of a value that may or may not exist, similarly, a future represents some async computation that may or may not succeed, a List represents zero or more items of some type. What you’ll notice is that all these data structures, encapsulate some sort of data model and have some well-defined API’s that help you manipulate their contents or effects :).
This post is going to try to simplify the concepts of monads, this means that a lot of technicalities may be omitted, but I’ll be sure to attach links to more technical concepts in this post.
Now, what on earth is a monad?. A more formal definition according to this is
A monad is a way to structure computations in terms of values and sequences of computations using those values. Monads allow the programmer to build up computations using sequential building blocks, which can themselves be sequences of computations. The monad determines how combined computations form a new computation and frees the programmer from having to code the combination manually each time it is required.