Clojure

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 →

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 →

Clojure Goodness: Finding The Maximum Or Minimum Value

Posted on by  
Hubert Klein Ikkink

To find the maximum or minimum value for numeric values we can use the max and min function. The functions accept one or more numeric arguments and the value that is maximum or minimum is returned. If the numbers are already in a sequence we can use apply max or apply min. If the values are not numbers we can use the max-key or min-key functions. These functions take as first argument a function that returns a number. So we can get the value that has the maximum or minimum return value for the function we pass as first argument.

In the next exmaple code we use the max, min, max-key and min-key functions:

Continue reading →

Clojure Goodness: Taking Or Dropping Elements From A Collection Based On Predicate

Posted on by  
Hubert Klein Ikkink

In Clojure we can take or drop elements from a collection based on a predicate using the functions take-while and drop-while. With the function take-while we take elements as long as the predicate returns true. Once the predicate returns false the function stops returning elements. Using the function drop-while we skip elements in the collection if the predicate returns true. If the predicate returns false the remaining elements in the collection are returned.

In the following example we use take-while and drop-while with different collection types:

Continue reading →

Clojure Goodness: Turn Java Object To Map With bean Function

Posted on by  
Hubert Klein Ikkink

The map data structure is used a lot in Clojure. When we want to use Java objects in our Clojure code we can convert the Java object to a map with the bean function. This function will use reflection to get all the properties of the Java object and converts each property with the property value to a key with value in the resulting map. The bean function will not recursively convert nested objects to a map, only the top-level properties are turned into key value pairs.

We see several examples of using the bean function in the following code snippet:

Continue reading →

shadow-left