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.
This post assumes a basic knowledge of Scala and SBT set up
Before we start, we need to know what exactly SBT is.
SBT stands for Scala Build Tool, as explained by the docs here, the next logical question should be “ What is a Build Tool ? “. If you did, great because I once asked the same question. A Build Tool as explained here is basically software that helps you simplify and automate most tasks that you use in your development process, from managing dependencies (other libraries you need) to tasks such as creating a JAR, pushing to docker e.t.c. Some really popular build tools include Maven, NPM e.t.c
SBT is pretty wide, and it cannot be covered it in a post. We won’t go too deep into the inner mechanics of SBT, because it’s quite complicated for beginners. We will just look at how to use it to make life easier for us :), So we’ll try to deal only with the basics and make it relatable.
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.