Sometimes we want to invoke Java methods from our Clojure code. If the Java method accepts a variable arguments (varargs) parameter and we want to invoke the method from Clojure we must pass an array as argument. To create an array in Clojure we can use several functions. The to-array function will transform a collection to an Object[] type. For primitive type arrays we can use for example int-array to get a int[] array. The function into-array is the most flexible function. This function accepts a sequence argument and optionally the class type of the resulting array. Once we have the array we can use it as argument value for the varargs parameter of the Java method we want to invoke.
In the following example we use into-array, to-array and short-array to invoke a Java method with varargs parameter and see how we can build different array types:
Continue reading →
The Java Stream API has many useful methods. If we want to partition a stream of objects by a given predicate we can use the partitioningBy() method from the java.util.stream.Collectors package. We must use this method in the collect() method of the stream. The result is a Map with the keys true and false. The objects from the stream that are true for the predicate will end up in the true value list and if the result of the predicate is false the value will end up in the list of values for the false key. The partitionBy method accepts a collector as second parameter. This collector will be applied to the values before they are put in the true or false keys in the result.
In the following example we use the partitioningBy method with different streams:
Continue reading →
The Java Stream API has many useful methods. If we want to transform a Stream to a Java array we can use the toArray method. Without an argument the result is an object array (Object[]), but we can also use an argument to return an array of another type. The easiest way is to use the contructor of the array type we want as method reference. Then the result is an array of the given type with the elements of the stream.
This is very useful if we have a Java Stream and want to use the elements to invoke a method with a variable arguments parameter. In Java we can pass an array object as variable arguments argument to a method. So if we transform the Stream to an array we can invoke the method with that value.
Continue reading →
One of the first topics you will encounter when studying functional programming will probably be currying. For an imperative programmer not used to mathematical notations, chances are you will find the concept hard to grasp. Then let this be the day you will remember as the day you completely understood currying!
Continue reading →
When you maintain a large Java project for a longer period, the moments where you’re finally able to remove unused code can be very satisfying.
No more upkeep, library version migrations or dark corners to maintain, for code that’s no longer being used.
But finding out which parts of the code base can be removed can be a challenge, and tooling in this space seems not to have kept pace with recent development practices in Java.
In this post we’ll outline an approach to find unreferenced code with ArchUnit, which allows you to iteratively detect & delete unused code from your Java projects.
Continue reading →
In my life as a developer I tried to understand why people want to make certain choices for languages and tools. I don’t want to go pick a tool or language just because people order me to do that. I always wonder why they do that. Is it policy or is did they actually think of it.
I mean I understand why people want to use Typescript from certain perspectives.
Continue reading →
That said, Puppet shifted traditional infrastructure management more towards software development because we are developing Config as Code, even if it’s declarative.
This shift brings new challenges. A Puppet codebase can become too big, complex, and monolithic. We have to test our code, but how should we do it, and when?
We’ve seen many cases where there is no Unit test (Rspec) for puppet modules and manually executed Catalog compilation tests that could take hours or even days, depending on the size of the codebase.
Continue reading →
The Optional class has the orElse and orElseGet methods to return a value when the Optional object is empty. This is useful to return a default value for example. But there is a small difference between the two methods. The orElseGet method needs a Supplier argument that returns a value of the type of the Optional value. The Supplier is only invoked when the Optional value is empty. The statement passed as argument to the orElse method is always executed, even when the Optional value is not empty. Preferrably we should use orElseGet as it will only invoke statements if needed.
In the following example code we see when our method getDefaultGreeting is invoked by using orElse and orElseGet with an empty and non-empty Optional object:
Continue reading →
Since I’ve been working on a Mac, I replaced the default terminal with iTerm2.
It provides some nice features like searching, autocomplete, or allowing to see images in the terminal.
But this one is my favorite one, the undo close tab / session.
Don’t you just hate it when you have multiple terminal tabs open, and accidentally close one?
Just that one where you had an important process running, or tailing an error log?
I do! :)
This is just a short blogpost how iTerm2 helps me having more fun using my terminal.
Actually it’s not much of a blogpost, this provides some screenshots.
Sometimes an image says more than words.
Continue reading →