Scala, MongoDB, and Cats-Effect

MongoDB is an open-source database that uses a document-oriented data model and a non-structured query language. It is one of the most powerful NoSQL databases around today. In comparison to traditional SQL databases, MongoDB does not use the usual rows and columns to model its data; instead, it uses a BSON (Binary JSON) format to save the data (documents) in collections, where the basic unit of data consists of a set of key-value pairs.

For Scala, there are several MongoDB clients available, the most popular of which are the official MongoDB Scala Driver and ReactiveMongo.

Functors in Functional Programming


1. Overview

In this tutorial, we’ll take a look at Functor type class in Cats. The idea of Functor is “something that can be mapped over”, we’ll see what is actually mapped and how. In functional programming, Functors come into play when we have types or values wrapped inside contexts or containers. We don’t have to know any of the implementation details of those contexts or containers themselves.

2. SBT Dependencies

To start, let’s add the Cats library to our dependencies :

Transactional Effects in Scala

Introduction

This post was inspired by a StackOverflow answer for the question Compose Futures with Recovery in Scala: Is it possible to compose Futures in a manner where, if one of them fails, a previous Future's execution result will be rolled back via some function?

The Problem

The case with Future can be generalized to some effect F[_], so we can say: How can we compose effects F[_] in a manner where, if one of them fails, the previous F[_] execution result will be rolled back via some function? This behavior is similar to what we know as a Transaction.