Archive: April 2020

Clojure Goodness: Replacing Matching Values In String

Posted on by  
Hubert Klein Ikkink

We can search for a value in a string and replace it with another value using the clojure.string/replace function. The first parameter is the original string value that we want to replace parts of. The second parameter can be a string value or regular expression. The last parameter is the replacement value that can be a string value or a function that returns a string value. The function itself gets either a string argument if the match has no nested groups (when match is a regular expression) or a vector with a complete match followed by the nested groups when the match has nested groups.

In the following example we several invocation of the clojure.string/replace function with different arguments:

Continue reading →

Clojure Goodness: Getting Intersections Between Sets

Posted on by  
Hubert Klein Ikkink

In the clojure.set namespace we can find the intersection function. This functions accepts one or more sets as arguments and return a new set with all elements that are present in the sets that are passed as arguments to the intersection function. The argument must be a set, so we need to convert other collection or seq values to a set first before we use it as an argument for the function.

In the following example we use one, two or three arguments for the intersection function and also convert other types to a set to be used as argument:

Continue reading →

Clojure Goodness: Joining Elements in a Collection

Posted on by  
Hubert Klein Ikkink

We can use the join function from the clojure.string namespace to join elements from a collection into a string. We can optionally specify a separator that is used to separate each element in the string output. The separator is not used after the last element of the collection. If we don’t specify a separator the elements are concatenated without separation. The string representation for each element in the collection is used in the joined end result.

In the following example code we see different usages of the join function:

Continue reading →

How to fix a drop of code coverage with a multi module Maven project

Posted on by  
Jacob van Lingen

Adding code coverage to SonarQube is quite easy for any Maven project nowadays. Just add the jacoco-maven-plugin dependency to your pom.xml, add the prepare-agent execution task, and you are good to go. Even for multiple modules this works out of the box. But time goes on and your application grows as well. You start moving code to other modules, and somehow SonarQube no longer seems to pick up the covered code. What the heck is going on?

The answer is shamefully simple. You did not write proper unit tests, but component tests. JaCoCo assumes at default every module has its own set of tests. So, as soon as your test covers multiple modules, only the 'current' module is counted as covered code.

Continue reading →

Clojure Goodness: Trimming Strings

Posted on by  
Hubert Klein Ikkink

In the clojure.string namespace we can find several useful function for working with strings. If we want to trim a string we can choose for the trim, trial, trimr and trim-newline functions. To trim all characters before a string we must use the triml function. To remove all space characters after a string we use trimr. To remove space characters both before and after a string we can use the trim function. Finally if we only want to remove the newline and/or return characters we use the trim-newline function.

In the following example we use the different trim functions on strings:

Continue reading →

shadow-left