Suppose you have a simple date string: "2015-02-27"
Regular expressions allow to parse the date and extract parts thereof: (\d\d\d\d)-(\d\d)-(\d\d) defines a pattern
which matches the digits and defines capturing groups for the year, month, and day parts.
In Java, a simple example usage would look like this:
That’s better, but I personally don’t like how the group names pollute the pattern. Regular expressions are hard
to read already, the Java escape characters add yet more clutter, and the additional ?<group> constructs are a
kiss of death.
Let’s look at how the same problem can be tackled in Scala:
According to the docs, the canonical way
to create a Regex is by using the method r, which is provided implicitly for strings:
Note the triple quotes, which allow to use the backslashes without further escaping. While this is nice, the true
advantage of Scala is that regular expressions can be used as extractors in a pattern match. The pattern stays plain
and simple, and yet we have all the freedom to name groups as we please:
In fact, most Java monoglots have probably seen and written code like this so many times, that they don’t
even see the problem anymore. And yet, with Java 8, they have to deal with a solution: the Optional type.
The service returns an Optional<String> and the caller is working her way around it. From a complexity point of
view, the change is argueably not much of an improvement: We still need a mutable value variable, and the overall
logic is roughly the same. And yet, the expressiveness has improved. The signature of the service has clearer
semantics now. Originally, the caller of the service could not distinguish between “the implementor of the service
made a programming error” and “the value has not been found” – both were resulting in null. This is possible now
and – even better – the type system forces the caller to deal with absents values.
But wait, there’s more
Now that the semantics are straight, let’s make the value variable immutable and eliminate the if .. else
We are taking advantage of the fact that Optional behaves like a collection: If it is absent, it behaves like an empty list, the call to map is never executed, and the result is an empty Optional (which we handle via orElse). If the result is present, it behaves like a list with one element, which we trim and return.
Wrapping legacy code
Imagine that our original service interface is actually coming from a 3rd party library. You can’t simply change
all the signatures to properly return Option<T>. But you can wrap the service calls and create yourself a
Hilary is coding in Python, but I took her excellent examples as an inspiration to
further practice my Scala. Based on Breeze, a set
of libraries for machine learning and numerical computing, I wrote my first little
My classifier uses a
support vector machine (SVM)
to distinguish images of two distinct categories — one containing circles,
the other containing crosses. I used 10 different fonts to create the two sets of
I used the red component of each pixel’s RGB value as a feature vector. Given that
all images are in grey scale, this seemed like a good first choice. And indeed,
after training the SVM with 9 images of each category, the 10th image can be
classified with a probability of roughly 0.75.
I have just completed the migration of the old & slooow WordPress thingie to a blazingly fast
Octopress setup. The static files are now hosted via
GitHub Pages, and the new feed can be reached at netzwerg.ch/atom.xml.
Unfortunately, the Scala course at Coursera is over. It was really an
excellent experience and I am desperately waiting for part two. The lectures were a bit on the slow side (luckily the player
featured nice high-speed playback), but the assignments were rather challenging. As far as I know, the course material will
stay available indefinitely, so if you missed the course, you can still sign up and watch the videos or download the
exercises just for fun.
Due to the official code of honour, I am not allowed to discuss any actual code here, but the last assignment is still worth
mentioning: Based on the concept of lazy data structures, we were asked to find a generic solution to the Bloxorz game. When I initially read the assignment, I doubted that this would be
possible within a reasonable amount of time. But the exercises were so well crafted, that they guided us in a step-by-step
fashion – in the end, I managed to get the maximal score within less than two hours.
Inspired by the addictive block-rolling, I was recently playing around with CSS3. I really have no 3D experience whatsoever,
but here’s my quick shot at some basic transformations (webkit only; put focus on cube and press left/right arrows):
When building Scala projects, your best bets are to either use the official sbt, the
simple build tool which is actually not so simple, or Gradle, my personal preference.
Both of them are very much based on conventions, i.e. they require only minimal configuration if you stick to the rules of a
default project structure. But what is the default project structure? And how can you set one up with minimal effort?
Enter giter8: A command line tool to generate files and directories from templates
published on github.
The official sbt templates maintained by Typesafe can be found here (note
that they publish g8 templates for their complete stack, even for examples & tutorials).
Along with more than 40k others, I am in the second week of Odersky’s free online course titled “Functional Programming
Principles in Scala”. The video lectures are really well structured and very entertaining – it’s great to learn from
the master himself. Given that the course only requires 1 year of programming background, the assignments are not too
complex yet, but I have no doubt that things will get trickier soon!
Regarding tool setup: Don’t even download the Eclipse-based Scala IDE, it sucks big time. How can these people get
anything done with a piece of crap that crashes every few minutes, even with just a 2-file project?! Luckily, the
assignments can as well be completed with IntelliJ IDEA, with the community edition Scala
To end this post with some code, this is how the first exercise looks like:
The cool thing is that this code compiles – Scala method naming FTW!
There are many reasons why you might want to change an application’s icon. In my case, I wanted to be able to
distinguish the stable IntelliJ IDEA 11 version from an early access 11.1 build:
Here’s how I changed the default (blue) icon to the new (pink) one:
First, reveal the application’s installation location in Finder (from the Dock context menu: Options » Show in Finder).
From within Finder, invoke the context menu on the selected application, and Show Package Contents. Locate the icon file
(*.icns) inside Contents » Resources. Make a backup copy, edit the icon in your favorite image editor (I simply overlaid
a red color filter in Acorn), and save it as a *.png.
Now comes the tricky part: Apple’s icon format includes more than
just an image, it actually contains a collection of images in different sizes, resolutions & states. To create a new *.icns
file, launch Icon Composer (which ships with OSX). Open the original application icon to check which sizes your new icon
Create a new file, drag & drop your edited *.png to the required image size containers, and save the result.
Re-launch your application – tada!