Scala Cats How-To Guide

Cats is popular library for functional programming in Scala and in addition to great official documentation, there is a plethora of articles and blog posts describing this library in depth. I don’t really want to repeat myself, so I decided to give this blog post different approach, more like a how-to style, with set of common problems and how to solve them using Cats. I also won’t go in depth with explanations how used type classes and data types from Cats work, but instead I’ll put links to official documentation, which definitely will do better job :) I’ll also update this blog post in future if I’ll find next topics to add.

Uploading Package to Hackage

After few months I spent learning Haskell, I finally finished my very first real world Haskell project called Headroom. It’s basically a manager for license headers located in source code files (more about it in next blog post). When I reached some reasonable stability of the codebase, I decided it would be also nice to have it released on Hackage. I found the process not to be as straightforward as I expected, so I decided to sum up my experience in this blog post, both for future myself and for anyone else interested.

My VSCode setup for Haskell

When I started to learn Haskell, I hoped that I’ll be able to get similar IDE experience as I have with Scala and IntelliJ Idea. Unfortunately the opposite was true. There are some Haskell plugins for Idea, but none of them worked for me (mostly due to crashes or hangs) and they seem to be pretty immature as well. But I found pretty good feedback on using VSCode for Haskell development, so I tried it and with help of few extensions, you get pretty decent IDE-like experience with syntax highlighting, error reporting and code completion. In this blogpost, I’ll present my VSCode setup I use for (almost) everyday Haskell development.

Table of Contents in Hakyll

It took me several attemps to run and actually maintain some kind of personal blog, so I’m happy I finally settled down on this one, based on Hakyll. Main reason why Hakyll was that I wanted to learn Haskell and best way for me to learn something new is to apply it for some real world use case. After publishing first few blog posts I realized that some are pretty long with many headings and having table of contents would definitely be helpful for readers.

So I spent some time searching best solution and found that Pandoc (library used by Hakyll to convert Markdown to HTML) already contains built-in support for this. In this blog post, I’ll show how to implement simple table of contents for blog posts, that can be easily customized to your specific needs. Because the below solution is the one I use for my blog, you can also check full source code to get details about imported modules, etc.

Troubles with Future

Anyone who works with Scala for a while probably used, or at least seen the Future class. Being part of the standard library, Future provides a way how to express asynchronous computation or value. It’s used by many and many popular Scala libraries, such as Apache Spark, Akka or Slick, and the use of Future itself is also pretty easy. But it also comes with some drawbacks, caused mainly by its design. This blog post summarizes the most common pitfals and introduces Monix Task as more powerful and robust alternative.

Extracting case class field names with Shapeless

In my previous job, we were working on backend system that regularly fetched data from other services, both 3rd party and inhouse. These data were parsed and mapped into our internal data structure modelled with case classes. The fetching itself was usually done using some REST API and was mostly straightforward, except for one service, where we had to explicitly list all fields of given entity for which we wanted to fetch new data. This was done using REST call similar to this:

GET /entities/User?fields=id,name,surname

So based on the fact that this entity would be mapped in our backend as case class User, how can we get all its field names to build the above query? In this blog post, I’d like to share the simple solution, based on some generic derivation using Shapeless.

Using Sass with Hakyll

In last few years, CSS changed a lot. Many new features were added, such as Flexbox, Media Queries and animations, but other things, such as using some kind of variables or modules that would allow better organizing of source code are still not so great. This is where CSS preprocessors come into play. And since I always had great experience with Sass preprocessor, I decided to use if for this blog, written using Hakyll static site generator.

Unfortunately there’s no built-in support for Sass or any other CSS preprocessor in Hakyll, but adding it is pretty straightforward and doesn’t require much coding. In this blog post, I decided to sum up this process, both for future myself and for anyone else who’d try to solve the same problem.

Better 'toString' alternative for case classes

One of the great advantages of Scala’s case classes is that compared to regular classes, bunch of useful methods, such as equals, hashCode and toString, are automatically generated. The generated toString method is nice, because it includes actual field values of the displayed case class.

case class Test(name: String, value: Int) 
val test = Test("The Answer", 42)

test.toString   // Test(The Answer,42)

The above output is good, but for case classes with many fields, nested structures or just lots of similar fields, it can become not so clear which value belongs to which field, as shown below:

case class Config(poolSize: Int, maxConnections: Int, batchSize: Int, intervalLength: Int, maxTimeout: Int) 
val config = Config(45, 23, 10, 10, 5000)

config.toString // Config(45,23,10,10,5000)

Wouldn’t it help if we could also see field names for these values? In this article, I’ll show how this can be done without much boilerplate using Cats and Kittens libraries.

Haskell on Raspberry PI 4

Last year, I purchased the Raspberry Pi 3B+ for some hobby projects. Shortly after I started to learn Haskell, I was wondering if I could build and run Haskell software on the ARMv7 platform. So I made some attempts with my Raspberry but quickly realized that despite being able to run compiled binaries, the 1GB or RAM is just not enough to build them using the GHC. So I gave it up for a while.

Then the Raspberry Pi 4B came out, with option to have up to 4GB of RAM. So I purchased the 4GB model and tried again. This time, with greatly increased available RAM size, I was able to make things working. You can find the entire process described in steps bellow.