Alexander Torstling

Creative programmer

Read this first

Monads for Developers with a Background in OO

Monads for Developers with a Background in OO

This article will try to explain what Monads are from an OO perspective.
More specifically how Monads solves a class of problems typically
solved differently in OO. It assumes
that you are reasonably well-versed in Java 8 features such as
Optional and Streams.

Monads as an Abstraction

As you probably know, FP puts more emphasis than OO on immutable state and
pure functions. In OO we usually solve “secondary concerns” like logging and metrics by
using side-effects. A member function might write to stdout directly, or
use some sort of mockable “Logger” interface, which we can use to inject
mocks to verify correct behaviour:

public static int impureAdd(int i, int j) {
    System.out.printf("Adding %s and %s", i, j);
    return i + j;
}

In FP, this is not considered good design. Instead, you are encouraged to
have your functions...

Continue reading →


Live Edit in Vert.x 3 and IntelliJ

If you’re using Vert.x 3 and Handlebars you probably want live class, static resources and template reloads. This post will explain how to acheive that.

I’m running my app as follows, but any way of starting should work:

public static void main(String[] args) {       
    Vertx vertx = Vertx.vertx();
    vertx.deployVerticle(Application.class.getName());
}

How Vert.x Handles Files

First its’ important to understand that Vert.x will look through the classpath for resources, even if it looks like it’s only using files internally at first glance.
The way Vert.x finds resources in the classpath is through its FileResolver (snippets from Vert.x 3.2.1):

      // First look for file with that name on disk
    File file = new File(fileName);
    if (cwd != null && !file.isAbsolute()) {
      file = new File(cwd, fileName);
    }
    if (!ENABLE_CP_RESOLVING) {
      return file;
    }
...

Continue reading →


Protoc help

I find it hard to find the help for protoc online, so here is the help for libprotoc 3.0.0:

Usage: protoc [OPTION] PROTO_FILES
Parse PROTO_FILES and generate output based on the options given:
  -IPATH, --proto_path=PATH   Specify the directory in which to search for
                              imports.  May be specified multiple times;
                              directories will be searched in order.  If not
                              given, the current working directory is used.
  --version                   Show version info and exit.
  -h, --help                  Show this text and exit.
  --encode=MESSAGE_TYPE       Read a text-format message of the given type
                              from standard input and write it in binary
                              to standard output.  The message type must
                              be defined in PROTO_FILES or their
...

Continue reading →


Amazon Kinesis Garbage Data

Are you seeing “garbage data” coming out of Amazon Kinesis? Something which looks like a raw memory dump or just corrupted records? Chances are that you are writing data with the KPL, which performs automatic aggregation. Aggregation means writing Protobuf blobs with multiple sequential messages instead of one message at a time, and unfortunately this is not handled transparently to a consumer other than the KCL. The data is stored this way in Kinesis itself, and the consumer is supposed to detect and decode these bulk messages. This process is called de-aggregation.

If you want to handle the de-aggregation, you need to do it manually with the SDK or use the KCL. For lambda, there is also the kinesis-deaggregation project.

To deaggregate manually when using the SDK, use:

List<UserRecord> deaggregatedRecords = UserRecord.deaggregate(poteniallyAggregatedRecords);

Interrestingly...

Continue reading →


Linux 4.1 BT Crash

My Macbook Air 4,2 with Ubuntu 15.10 would start to a black screen. The boot process went so far that you could hear the ubuntu drums in the background, but other than that nothing was responding.

There is a kernel bug regarding this at ​https://bugzilla.kernel.org/show_bug.cgi?id=100651

I’m running refind, and tried to find ways of booting to console. What worked was pressing F2 twice for boot options, and add systemd.unit=emergency.target to the boot params. “text” boot param did not work, and I couldn’t find it documented anywhere.

Once booted, dmesg ended with
[ 774.467449] Bluetooth: hci0: BCM: Read verbose config info failed (-16) + a couple of more rows regarding wifi scanning. So Bluetooth was my primary suspect. I remounted the root FS with mount -o remount,rw /dev/sdaX / and edited /etc/modprobe.d/blacklist.conf to include a line blacklist btbcm.

Now the compy boots. The...

Continue reading →