Archive for the ‘programming’ Category

What is dependency injection?

April 18, 2018 Leave a comment

See this blog post: Dependency Injection Demystified. In short: “Dependency injection means giving an object its instance variables.”. For a slightly longer explanation, read the blog post.

Reddit comments are here.

Categories: programming Tags: , ,

Advent of Code 2017

December 25, 2017 Leave a comment

This year I jumped into Advent of Code again. It was a great fun and I was waiting the new exercises every morning. Today I finished it:

This year I solved all the exercises in Kotlin. I will publish my solutions on GitHub soon. Update: here is the repo.

Here is a link to the Twitter page of Eric Wastl, to whom we can thank the Advent of Code. Thanks, Eric and keep up the good work!


Categories: kotlin, programming Tags: , ,

working with hex grids

December 11, 2017 Leave a comment

Solving Advent of Code 2017, today (Day 11) I ran into an interesting problem. We are on a (flat topped) hex grid, and we walk around. Question: how far did we get from the starting position? In short: what is the distance between two arbitrary hexagons in a hex grid?

I’ve never worked with a hex grid before so I had to do some research. I found an excellent blog post here: Hexagonal Grids. This post is interactive, so don’t forget to move your mouse over the figures.

I figured out that I needed cube coordinates; that gives the easiest way to calculate the distances. Here is a code snippet in Kotlin:

data class Point(val x: Int, val y: Int, val z: Int) {

    fun distance(b: Point): Int {
        return (abs(x - b.x) + abs(y - b.y) + abs(z - b.z)) / 2


var x = 0
var y = 0
var z = 0

val p1 = Point(x, y, z)    // starting position

val steps = "ne,ne,ne".split(",")    // go to North-East 3 times

for (direction in steps) {
    when(direction) {
        "n"  -> { ++y; --z }
        "ne" -> { ++x; --z }
        "se" -> { ++x; --y }
        "s"  -> { --y; ++z }
        "sw" -> { --x; ++z }
        "nw" -> { ++y; --x }

val p2 = Point(x, y, z)    // we arrived here
val dist = p1.distance(p2)    // the result is here (in this example: 3)
Categories: kotlin, programming Tags: ,

Getting started with C# on Linux

June 30, 2017 Leave a comment

I’ve heard lots of good things about C# but I’ve never tried it. I saw some codes and thought “fine, it’s like Java”. As I also have Linux on my primary machine, I was not interested in Microsoft technologies. However, a few years ago .NET Core was open sourced and it reached version 1.0 (now it’s 1.1). It’s a cross platform framework and it works well under Linux too, so I thought it was time to try it. Project Mono has also existed for a long time but .NET Core comes directly from Microsoft.

Under Manjaro, install these packages with yaourt: dotnet, dotnet-cli, dotnet-sdk. We will also try Mono, so install the package mono too. For Ubuntu, see the instructions here: .

Create a project

Create a folder for holding your C# projects, e.g. ~/CSharpProjects (optional), and create a folder for a sample project (e.g. ~/CSharpProjects/Sample). Enter it, and create a source file called hello.cs with the following content:

using System;
using static System.Console;
using System.Linq;

namespace SampleApp
    class MainClass
        public static void Main(string[] args)
            WriteLine("Hello World!");

Compile with Mono

If you installed Mono, you have the command “csc” (C Sharp Compiler).

$ csc hello.cs
$ mono hello.exe 
Hello World!
$ chmod u+x hello.exe
$ ./hello.exe 
Hello World!

Compile with dotnet

Now let’s use the official command-line tools. You can also delete the file “hello.exe”. I suppose you are in the project’s folder. Issue the following command:

$ dotnet new console

It creates a project file (with extension .csproj), and a sample file called Program.cs . As we already have hello.cs, Program.cs is not needed so simply delete it (leave only one source file, but it doesn’t matter which one). Then,

$ dotnet restore    # installs some necessary packages, using the .csproj project file
Restoring packages for /home/jabba/Dropbox/csharp/Sample/Sample.csproj...
$ dotnet run        # compile and run
Hello World!

The binary output is here: bin/Debug/netcoreapp1.1/Sample.dll . If you use the command “dotnet”, you get a .dll, not an .exe . However, it can be executed:

$ cd bin/Debug/netcoreapp1.1
$ ./Sample.dll 
Hello World!
$ dotnet Sample.dll 
Hello World!

When you are ready with the development and ready for deployment, issue this command:

$ dotnet publish
Microsoft (R) Build Engine version 15.1.1012.6693
Copyright (C) Microsoft Corporation. All rights reserved.

  Sample -> /home/jabba/Dropbox/csharp/Sample/bin/Debug/netcoreapp1.1/Sample.dll

According to the docs, “dotnet publish compiles the application, reads through its dependencies specified in the project file, and publishes the resulting set of files to a directory. The dotnet publish command’s output is ready for deployment to a hosting system (for example, a server, PC, Mac, laptop) for execution and is the only officially supported way to prepare the application for deployment.”


Mono ships a REPL too called “csharp”.

$ csharp 
Mono C# Shell, type "help;" for help

Enter statements below.
csharp> 1+1

Visual Studio Code support

Programming C# with VS Code is a joy. When you open a .cs file, the editor offers immediately to install the official C# extension. In order to execute a program, install the Code Runner extension. Then, add the following lines to your user settings:

    "code-runner.executorMap": {
        // "python": "python3",
        // "csharp": "echo '# calling mono\n' && cd $dir && csc /nologo $fileName && mono $dir$fileNameWithoutExt.exe",
        "csharp": "echo '# calling dotnet run\n' && dotnet run"

Choose either Mono or dotnet.


If you have Linux, no problem, you can try C#. I started to play with it yesterday, but I like it. It seems a saner language than Java :)



An awesome book for Scala

March 10, 2015 Leave a comment

scalaI decided to learn Scala as a new language. I’ve heard a lot of good things about it. I looked at some basic stuffs in it and it seems to be a cool language. It’s based on the JVM, thus it stands on the shoulders of giants :)

I found an excellent book for learning it called “Scala for the Impatient” (Amazon link). Here you can download the first 9 chapters legally, and they nicely cover the basics that let you get started. I just finished the first chapter (13 pages) and it teaches you a lot. At the end of each chapter there are exercises, so you can test your knowledge and you have a feedback of your progress. I think a programming book without exercises is worthless.

Last week I started to work with the book “Programming in Scala” 2nd ed., which is written by the author of the language. I read the first four chapters (116 pages) but it goes in all directions. It talks about advanced stuffs at the beginning and it doesn’t explain the basics well. After the four chapters I had the impression that I still know nothing about Scala…

“Scala for the Impatient” seems better to my taste. I should buy the complete book.

Programming in a nutshell

February 14, 2014 Leave a comment

This GIF sums it up:

Found it on reddit.

Categories: fun, programming Tags:


October 26, 2013 Leave a comment

I’ve never used Lua but I heard a lot about it. Here is a post that lists its good and bad parts:

Lua: Good, bad, and ugly parts

Categories: programming Tags: