Archive: May 2020

Clojure Goodness: Using The range Function

Posted on by  
Hubert Klein Ikkink

In Clojure we can use the range function to create a lazy sequence of numbers. We can optionally specify a start value, end value and define the steps between the numbers. If we use the end value argument that value is exclusive for the returned values in the lazy sequence.

In the following example we invoke the range function with different arguments:

Continue reading →

Visualizing a nasty virus

Posted on by  
Jacob van Lingen

At the time of writing the coronavirus is raging the earth. Very soon after the outbreak, visualizations of both the virus and the effect of the disease started to appear everywhere. As I partially graduated in the subject of data visualization, I have always been interested in those graphs. Lately, I followed an introduction course to visualize data with D3.js. After I completed this course, I wanted to draw some meaningful graphics with this library. So follow along when I explain a little bit about D3 and then draw a simplified version of the coronavirus molecule.

Continue reading →

Clojure Goodness: Set Default Value For nil Function Argument With fnil

Posted on by  
Hubert Klein Ikkink

The function fnil can be used to create a new function from an existing function where we define default values for arguments that can be nil. Especially when we want to use a function that we didn’t write ourselves this can be helpful to handle nil values in a consistent way. We can prevent for example a NullPointerException by setting a default value. We can define default values for a maximum of three arguments for the original function. When we invoke the function that is returned from the fnil function any extra arguments after the first arguments are simply passed on to the original function.

In the following example code we define a new times function based on the * function. In the example we define a default value for the first three arguments.

Continue reading →

Building a Magic Mirror

Posted on by  
Justus Brugman

Now it’s time for something completely different. Lately I’ve been watching some great videos from Matthew Perks, he has a YouTube channel called DIY Perks. This inspired me to pick up my own little project to create our own Magic Mirror.

Well, there is not so much magic going around here. A better name would be a Smart Mirror. Basically it is nothing more than a monitor behind a double-sided mirror, powered by for example a Raspberry Pi. A double-sided mirror is not a regular mirror, but it’s reflecting coating has about 70% reflection and 25% light transmission. So if you place the 'glass' on a dark surface, you would see just a mirror. When you however put a light source behind it, you’re able to see through it. If you put a black page on the monitor, add some widgets on them, they seem to be part of the mirror’s reflection. A Magic Mirror is a great and simple example of an 'internet of things' device.

Continue reading →

Clojure Goodness: Partition Collection Into Sequences

Posted on by  
Hubert Klein Ikkink

Clojure has the partition, partition-all and partition-by functions to transform a collection into a list of sequences with a (fixed) number of items. We can set the number of items in each sequence by providing a number as the first argument of the partition and partition-all functions. Any remainder elements are not in the resulting list of sequences when we use partition, but are when we use partition-all. We can also specify another collection to use values from to fill up the remainder as the third argument of the partition function.
Optionally we can specify an offset step value as a second argument using both functions. This mean a new partition sequence will start based on stepping through the original collection with the given step value.
Finally we can use a function to define when a new partition must start with the partition-by function. Every time the function returns a new value a new partition will begin.

In the following example Clojure code we use all three functions with all possible arguments:

Continue reading →

Awesome Asciidoctor: Image As Link Reference

Posted on by  
Hubert Klein Ikkink

To make an image linkable in Asciidoctor when formatted to HTML we must use the link attribute when we use the image macro. The value of the link attribute is the address where the user goes when clicking on the image. We can also specify extra link attributes like window to specify the target window for the link to open in.

In the following example we use the link attribute for a block and inline image, with and without an extra window attribute:

Continue reading →

Clojure Goodness: Check Substring Is Part Of String

Posted on by  
Hubert Klein Ikkink

Sometimes we want to see if a string is part of another string. Or if the string value starts or ends with a certain string. In Clojure we can use the includes? function from the clojure.string namespace to check if a string is part of another string value. To see if a string starts with a certain value we can use the starts-with? function from the clojure.string namespace. And to check if a string ends with a given value we use ends-with? from the same namespace.

In the next example code we use these functions and also add a matches? function to check if a string matches with a regular expression defined in a string:

Continue reading →

Don't be afraid of Docker

Posted on by  
Jacob van Lingen

As a developer, you are familiar with Docker. You push your images to the Hub, use Compose locally and know a thing or two about Kubernetes. Or…​ Well…​ To be honest…​ You don’t. And you are ashamed you don’t know anything about it. You browse the internet and it’s so overwhelming. So you stop looking and continue what you’ve been doing all the time. Deep inside, you still wonder. Can’t anyone not just explain Docker in simple terms? Is it really this hard? Or am I just missing something really obvious?

Continue reading →

Clojure Goodness: Getting Results From Multiple Functions With Juxt Function

Posted on by  
Hubert Klein Ikkink

The function juxt can be used to get results from multiple functions on the same argument in one go. We pass the functions we want to invoke as arguments to the juxt function. This results in a new function that will return a vector with the results from each original function that is passed to the juxt function. So the first element in the result vector is the result of the first function, the second element is the result of the second function and so on. The documentation of the juxt function shows this clearly as juxt a b c ⇒ [a(x) b(x) c(x)].

In the following example we use the juxt function to apply multiple functions on a string, collection and map:

Continue reading →

Practical Money in Java

Posted on by  
Peter Steman

Introduction

While investigating Protobuf I shot from the hip and wrote that: to model money, one should use BigDecimal. That’s the conventional wisdom and it is correct - in a lot of cases. It’s fine when you deal with one single currency and you are most of all concerned with the precision of financial calculations. But what are some other options? let’s find out.

Continue reading →

Clojure Goodness: Reapply Function With Iterate To Create Infinitive Sequence

Posted on by  
Hubert Klein Ikkink

The iterate function create a lazy, infinitive sequence based on function calls. The iterate function takes a function and an initial value as arguments. The first element in the sequence is the initial value, next the function is invoked with the previous element as argument and this continues for each new element. Suppose we have a function #(+ 2 %) that adds 2 to the input argument. Then if we use this function with iterate and start with value 1 the first elements of the sequence will be 1, (+ 2 1), (+ 2 3), (+ 2 5). So first element is the initial value, next element is the invocation of the function with input argument 1. The result of this function is 3, which is then the input for the function to calculate the next element and so on.

In the following example code we use iterate in different scenario’s:

Continue reading →

shadow-left