Posts by Hubert Klein Ikkink

Clojure Goodness: Remove Duplicates From A Collection With distinct

Posted on by  
Hubert Klein Ikkink

With the function distinct we can remove duplicate elements from a collection. The function returns a lazy sequence when we use a collection argument. Without arguments the function returns a transducer. When we want to remove duplicates and we don’t need the lazy sequence result we could also turn a collection into a set with for example the set or into functions.

In the following example we use the distinct function on several collections.

Continue reading →

Clojure Goodness: Remove Consecutive Duplicate Elements From Collection

Posted on by  
Hubert Klein Ikkink

The Clojure core namespace contains many functions. One of the functions is the dedupe function. This function can remove consecutive duplicates from a collection and returns a lazy sequence where only one of the duplicates remain. It will not remove all duplicate elements from the collection, but only when the element is directly followed by a duplicate element. The function returns a transducer when no argument is given.

In the following code sample we use the dedupe function on several collections:

Continue reading →

Clojure Goodness: Use .. For Invocation Java Method Chaining

Posted on by  
Hubert Klein Ikkink

Accessing Java from Clojure is easy. With the dot (.) special form we can invoke for example methods from a Java class or instance. If we want to invoke several methods together where the return value from one method is used to invoke the next method (method chaining) we can use the .. macro. The macro will expand into a nested expression with the . forms.

In the following example we see how to use the .. macro and how we can achieve the same result using nested . expressions and by using the thread first macro:

Continue reading →

Clojure Goodness: Invoke Java Method With Varargs Parameter

Posted on by  
Hubert Klein Ikkink

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 →

Java Joy: Partition Stream By Predicate

Posted on by  
Hubert Klein Ikkink

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 →

Java Joy: Turn Stream Into An Array

Posted on by  
Hubert Klein Ikkink

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 →

Java Joy: Optional orElse orElseGet That Is The Question

Posted on by  
Hubert Klein Ikkink

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 →

Clojure Goodness: Getting Part Of A Vector With subvec

Posted on by  
Hubert Klein Ikkink

In Clojure we can get part of a vector collection using the subvec function. The function takes a vector as argument, a required begin index and optional end index. The returned value is a vector with part of the values of the original vector starting from the begin up to the end index. If we leave out the optional end index, the size of the vector is used as end index.

In the following example we use the subvec function with and without the end index:

Continue reading →

Clojure Goodness: Split Collection With Predicate

Posted on by  
Hubert Klein Ikkink

To split a collection in Clojure we can use the split-with and split-at functions. The split-with function takes a predicate as first argument and a colletion as second argument. The function will return a vector with two items. The first item is the result of the function take-while with the given predicate. The second item in the result vector is the resul of the drop-while function with the same predicate.

We use the split-at function with a number as first argument followed by a collection to split based on a given number of items. Instead of using a predicate we can define the number of items that we want as the first item in the result vector. The first item in the result vector is the result of invoking the take function. The resulting number of items of the collection will be the second item in the result vector and is achieved by invoking the drop function.

Continue reading →

Clojure Goodness: Shuffle A Collection

Posted on by  
Hubert Klein Ikkink

In Clojure we can use the shuffle function with a collection argument to get a new collection where the items of the input collection are re-ordered randomly. The function delegates to the Java java.util.Collections#shuffle method.

In the following example code we use the shuffle method:

Continue reading →

Clojure Goodness: Formatting With Java Format String

Posted on by  
Hubert Klein Ikkink

In Clojure we can format a string using Common Lisp format syntax or the Java format string syntax. In the post we will look at the how we can use the Java format string syntax. We must use the format function in the clojure.core namespace. The method delegates to the standard JDK String#format method. The first argument is a format string followed by one or more arguments that are used in the format string. We can look up the syntax of the format string in the Javadoc for the java.util.Formatter class.

In the following example code we use the format function with different format strings:

Continue reading →

Clojure Goodness: Formatting With Java Format String

Posted on by  
Hubert Klein Ikkink

In Clojure we can format a string using Common Lisp format syntax or the Java format string syntax. In the post we will look at the how we can use the Java format string syntax. We must use the format function in the clojure.core namespace. The method delegates to the standard JDK String#format method. The first argument is a format string followed by one or more arguments that are used in the format string. We can look up the syntax of the format string in the Javadoc for the java.util.Formatter class.

In the following example code we use the format function with different format strings:

Continue reading →

shadow-left