Latest blog posts

Generating SQL queries with shapeless

Scala, shapeless

In the previous posts we created the SqlSaver class which can set values into the prepared statement. It assumes that the SQL request is correct and the parameters are in the required order (in the order they are defined in the model). What if the model class is changed? If the query is not updated we’ll get a runtime error, since the fields order in the query and the order of calls performed by SqlSaver are not the same anymore. So, it would be nice to generate SQL queries as well. Something like:

val query = StatementGenerator[Sale].insert(tableName)
val statement = connection.prepareStatement(query)
SqlSaver[Sale](statement, 1)(sale)

Let’s try to implement it with shapeless.

Continue reading

Firefox ESR and XMonad

XMonad, Gentoo

If you are XMonad and Firefox ESR user, just like me, you might know that there is a problem with full screen videos playback. A video hangs and you cannot close it. All you can do is just to kill Firefox. It’s already fixed in the main line, but it looks like they don’t want to port the patch to ESR version.

Fortunately the patch is not too big and I adopted it for the ESR version. Here is it:

Continue reading

Pagination with Hakyll

Hakyll, Haskell

Yesterday I realized that there are more than ten posts in my blog. Not so many for more than a year, but quite a lot for the index page. The simplest solution is to just limit amount of posts with take function, and write something like “more posts in the archives”. But I prefer to have pagination for the index page. Hakyll has built-in support for pagination in the module Hakyll.Web.Paginate. There is a very nice manual about how to use it in this blog post.

Unfortunately, Paginate provides only first/previous/current/next/last functionality. It’s quite common to have only “Older posts” and “Newer posts” buttons for blogs, but I’d like to have a list of all pages in the Bootstrap pagination component. So, it’s time to write some Haskell code (of course you need to write code to add the standard Paginate, but I’m going to write a little bit more).

Continue reading

Fixing bugs in SqlSaver

Scala, shapeless

In the previous post I created SqlSaver class. Later, playing with it, I found that it has several bugs.

First of all, it doesn’t work properly with nested classes. Let’s start with a test:

case class SaleRecord(id: Int, sale: Sale, seller: String)

it should "save nested case classes" in {
  val date =
  SqlSaver[SaleRecord].save(stm, 6)(
    SaleRecord(1, Sale("bar", date, 42), "Shop")
  ) should equal(11)

  verify(stm).setInt(6, 1)
  verify(stm).setString(7, "bar")
  verify(stm).setTimestamp(8, Timestamp.valueOf(date))
  verify(stm).setBigDecimal(9, java.math.BigDecimal.valueOf(42))
  verify(stm).setString(10, "Shop")

Unfortunately it doesn’t compile:

[error] SqlSaverTest.scala:38: diverging implicit expansion for type SqlSaver[LocalDateTime :: BigDecimal :: HNil]
[error] starting with method hlistSaver in object SqlSaver
[error]    SqlSaver[SaleRecord].save(stm, 6)(
[error]            ^
Note: here and later I rewrote HLists into the infix form, for readability.
Continue reading

Getting started with shapeless

Scala, shapeless

Sooner or later, every Scala programmer tries to study shapeless, Scalaz, Cats and other libraries, which are not designed to solve one small problem, but were created to explode your brain change the way you are writing your code, make it safer and at the same time more generic. I’ve tried to study shapeless several times, but the main problem is that there are no entry point. There are a lot of things and all of them are quite difficult.

Finally I decided to solve some small problems with shapeless to find some patterns and scenarios of how to use it. So, what kind of problems can I use? Shapeless is really useful when you want to process your data in a type safe generic way.

The problem I’d like to solve in this post is type safe saving arbitrary case class into an SQL statement. E.g:

case class Sale(name: String, date: LocalDateTime, price: BigDecimal)

SqlSaver[Sale].save(st, 1)(Sale("Banana",, 55))

This call will call the PreparedStatement methods for each field, taking into account the field type. In this example it should be:

st.setTimestamp(2, Timestamp.valueOf(
st.setBigDecimal(3, sale.price.underlying)
Continue reading

Scala type variance and Java collections

Scala, collections, Java

Converting Scala collections to Java and backward

It’s quite often that we need to use a Java API from Scala. And these usages occurs even more often when you have modules written in Java in your Scala project. The most common problem is to pass Java collections to Scala API and Scala collections to Java. Fortunately, Scala library provides bidirectional implicit converters between Scala and Java collections. There are two classes JavaConverters and JavaConversions which provide the same functionality but in a different way.

Assume we have a Java service:

interface Message {
    String getText();
    LocalDateTime getDate();

public class JavaService {
    void handleMessages(List<Message> messages) {
            .sorted((o1, o2) -> o1.getDate().compareTo(o2.getDate()))
            .forEach(m ->
                    + " " + m.getText()));

    List<Message> generateMessages(int n) {
        return IntStream.range(0, n)
                .mapToObj(i -> new JavaMessage(String.valueOf(i),
Continue reading

A little bit more about git cherry-pick


In one of the previous posts I talked about a way to copy a bunch of commits from one git branch to another one. Today had to copy commits again, but for a different reason.

Several days ago I started a new project in a new repository. I made several commits on my master branch when I realized that I needed to create a pull request to perform a code review. What I had:

A -- B -- C -- D -- E                                          master

What I’d like to have:

X                                                              master
  A -- B -- C -- D -- E                                        feature
Continue reading

Easy JSON analyze with spray-json

Scala, Akka, Spray, DSL

If you use Spray or Akka HTTP to create a REST service, possibly you need to work with JSON objects. Both Spray and Akka HTTP have built-in support of spray-json.

In most cases you have a fixed set of fields in your JSON API, so the proposed way to work with spray-json is to create model case classes and marshallers/unmarshallers for them:

case class Person(name: String, age: Int, title: Option[String])

trait PersonRoutes extends DefaultJsonProtocol with SprayJsonSupport {
  implicit val personFormat = jsonFormat3(Person)

  val route =
    put {
      path("people") {
        entity(as[Person]) { person =>
          // Store to db
Continue reading

Batch cherry-picking with git


Today I came to work and found that I have to move 18 commits from my development git branch to the old release. I’ve worked on one feature for several weeks, and kept my branch up to date with master, so the history looked like this:

               Z -- Y -- X -- W -- V -- U -- T -- S            feature
              /         /              /
-- A -- B -- C -- D -- E ----- F ---- G -- H                   master
     R -- Q                                                    release

So, my goal is to create a feature' branch on top of the release and copy the commits Z, Y, W, V, T and S there:

Continue reading

Draft posts with Hakyll

Hakyll, Haskell

If you’d like to write a long posts in your Hakyll blog, you often can find that you’d like to commit a post (or several posts), but you’re are not going to deploy it on server. Here is my solution inspired by this post. Now I can call Hakyll with --with-drafts and it will use both posts and drafts directories to collect posts data.

There are several issues I had faced with when I was solving this problem:

  1. Detect what program is running in the draft mode.
  2. Pass posts to Hakyll.
  3. It is safer to have a separate folder for generated site while running in the draft mode to avoid the draft articles deploying by mistake.
  4. Hakyll itself uses cmdargs package which checks if the command is executed with proper options.
Continue reading

Recent posts

Tag cloud

Akka DSL Gentoo Hakyll Haskell Java NVidia Scala Spray XMonad collections git shapeless