Practically Understanding UTF-8 encoding

If you save a text file with non-english characters and wondered how the editor knows how to interpret it correctly, then this is for you.

The Unicode standard defines a set of rules that govern how text is encoded and stored as bytes, such that it can be read back preserving the information that was encoded. In this tutorial, we’ll particularly look at UTF-8.

In UTF-8, characters are given a number, also known as code point, and these code points are grouped into what we will call code blocks for simplicity. The code block determines to an extent, how many bytes will be used to store the data.

Now, let’s see what this means in practice. if we open a notepad and type in the word “hello”, save the file as a text file (“sample.txt”), and check the file size, we will see it’s 5 bytes which makes a little sense as there are only 5 characters:

Continue reading “Practically Understanding UTF-8 encoding”

Essential Effects 00: The book

Here are my notes from the book Essential Effects .

I really love this book as it explained a lot of concepts that are used in functional programming and the cats effect ecosystem.

I love how the examples were concise and practical.

I Would have loved if there was more on Testing effects, but there was an introductory chapter.

I totally recommend this book for anyone who wants to dive deeper into functional programming principles and understand more of the cats effect library

Essential Effects 04: Granular Parallelism (Fibers)

Forking, Joining and cancelling an effect

As explained by the use of ParallelIOwhich is more or less the effectful form of Futures.
It thus means that there has to be a way to run effects concurrently and/or in parallel.

It means there must be a wy to run our effect on another thread, wait for it to finish, and
continue execution.

To explain this, we will try to rewrite the parMapN function that comes with cats effect, but we”ll call ours myParMapN with the following signature:

Continue reading “Essential Effects 04: Granular Parallelism (Fibers)”

Essential Effects 05: Contexts and Thread Pools

CPU vs IO Bound work

IO Bound work

IO operations typically involves reading or writing to files, databases, sockets e.t.c. Most of these times, waiting is involved as sometimes data isn’t yet available.
In these scenarios, the CPU will have to wait until the underlying hardware has delivered the data since it is not immediately available.

Any IO operation that requires reading or writing using anything that is not stored in RAM
will cause something called IOwait, which is basically a system call that tells the CPU to pause the execution og the current thread until data is available or data has been successfully transmitted. This way. the CPU can easily pause that thread and move on to do other things.

Continue reading “Essential Effects 05: Contexts and Thread Pools”

Essential Effects 06: Integrating with Legacy systems

Integrating Asynchrony

We’ve dealt a lot with IO values, but in practice, we may have to deal with legacy
codebases that use futures or other async structures. But since we are writing pure functional programs, we need a way to be able to capture or lift the result of these asynchronous computations into an IO.

Cats effect provides that capability via the IO.async method. The way this works is that the method takes a function from a callback to Unit, where the callback itself is a function from an Either[Throwable,A] to Unit.

The function signature looks like this

Continue reading “Essential Effects 06: Integrating with Legacy systems”

Essential Effects 07: Managing Resources

Managing Resources

In Cats Effect, the Resource data type represents this acquire-use-release pattern to
manage state. In other words, a Resource represents acquisition of an entity with its release function already implemented, which will be called once that resource has been used.

To acquire and use a resource, we call Resource.make which has this type signature:

Continue reading “Essential Effects 07: Managing Resources”