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

After several attempts to run and actually maintain some personal blog, I finally settled down on this one based on Hakyll. Main reason was that I decided to learn Haskell and the best way for me how to learn new technology is to use it for some real world application. After publishing several blog posts I realized that some of them are pretty long with many headings and having some kind of table of contents would definitely help to navigate them.

I spent some time by searching optimal solution and found that Pandoc, library used by Hakyll to convert Markdown to HTML, already contains pretty decent built-in support for this. In this blog post, I’ll demonstrate how to implement simple table of contents for blog posts, that can be easily customized to your specific needs.

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.