Blog posts, page 2

Getting started with 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

, ,

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

, , ,

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


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

Fixing nvidia-drivers-340.76 build with kernel 4.0


After upgrade to gentoo-sources-4.0.5 I’ve found what I cannot install drivers for my good old GTS-250. This card is pretty old, so it supports only in 340.x branch of NVidia drivers. Unfortunately NVidia is not really fast in fixing of such kind of problems ( They just provide a link to the patch in Ubuntu. But, AFAIK Gentoo maintainers doesn’t take such patches as well (I suppose it is break older kernels compatibility).

The cool thing I’ve never used before called epatch_user allows you to add custom patches to any ebuild supporting this feature. And actually this is the way, proposed to solve such issues. So, all you need to do is to create folder /etc/portage/patches/x11-drivers/nvidia-drivers-340.76 and place there following patch:

--- kernel/nv-pat.c.orig    2015-08-17 21:46:15.541979210 +0300
+++ kernel/nv-pat.c 2015-08-17 21:47:17.180978210 +0300
@@ -35,8 +35,8 @@
     unsigned long cr0 = read_cr0();
     write_cr0(((cr0 & (0xdfffffff)) | 0x40000000));
-    *cr4 = read_cr4();
-    if (*cr4 & 0x80) write_cr4(*cr4 & ~0x80);
+    *cr4 = __read_cr4();
+    if (*cr4 & 0x80) __write_cr4(*cr4 & ~0x80);
@@ -46,7 +46,7 @@
     write_cr0((cr0 & 0x9fffffff));
-    if (cr4 & 0x80) write_cr4(cr4);
+    if (cr4 & 0x80) __write_cr4(cr4);
 static int nv_determine_pat_mode(void)

Putting posts content on the index page


Once you have created a Hakyll based site the first page contains only the titles of your posts. It can be acceptable if your first page contains another content, but if there are only the latest posts it looks weird. Let’s check how does it works.

Default index page template includes post-list.html template which contains following code:

      <a href="$url$">$title$</a> - $date$
Continue reading

Installing Hakyll on Gentoo Linux


I have been using Gentoo Linux as my primary OS since 2004. So, it usually important for me to choose software which available in Gentoo portage. And there is no Hakyll in portage :(

Of course, it is possible to install Hakyll from cabal using cabal-install, but according to the wiki this is not recommended. Instead of this, we can install this package fro the Gentoo Haskell overlay. So, first of all it required to add this overlay using layman (of course if you didn’t do it before).

$ emerge layman
$ layman -a haskell
$ echo "/var/lib/layman/make.conf" >> /etc/portage/make.conf
Continue reading

Why Hakyll?

Well, I thought about personal blog for a quite long time, but to be honest I’m really lazy. And the second thing, I don’t like most of CMS I’ve ever seen. So, the idea of static generation is looking much more suitable for this kind of stuff. But I never tried it before (to be honest I’ve never heard about it, except 1C Bitrix. At least I thought it generates a lot of static pages, but it still requires PHP).

Finally, I found out that there several solutions for site generation (thanks Vitaly Repin), like (Jekyll, Hakyll, Middleman). The interesting thing, is that if you try to google something like “Jekyll vs Hakyll”, there will be several posts about migration from Jekyll to Hakyll. Of course, it doesn’t mean that Hakyll is better, but it seems like it is more flexible.

Another reason why I’ve chosen Hakyll is Haskell. Haskell was a language which bring me to the functional programming world. It was like a magic for me (and it steel so, since I’ve never participated a real projects in Haskell). It feels like you just have written your first program, and it’s working (of course if you was able to compile it :)).

So, the first impression is “I like it”. Even thought, it’s not strictly required to know Haskell, and all of this stuff (like Monads and Monoids), this knowledge gives you some advantages, and it gives me a hope that I’ll be able to make this blog much better that it is now :)