Clojure

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 →

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 →

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 →

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 →

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 →

Clojure Goodness: Combine First And Next Functions Multiple Times

Posted on by  
Hubert Klein Ikkink

The first function in Clojure returns the first item of a collection. The next function returns a new sequence with all elements after the first element from a collection. Clojure adds some utility methods to combine first and next with different combinations. We can use the function ffirst which is will return the first element of the first element of a collection and the nfirst function to get the next elements from the first element of a collection. We can use the function fnext to get the first element of the next elements of a collection and the function nnext to get the next elements of the next elements of a collection.

In the following example we use the ffirst, nfirst, fnext and nnext:

Continue reading →

Clojure Goodness: Checking Predicate For Every Or Any Element In A Collection

Posted on by  
Hubert Klein Ikkink

In Clojure we can use several functions to see if at least one or all elements in a collection return true or false for a predicate. The function every? only returns true if the predicate function returns true for all elements in the collection. To function not-every? return true if a predicate function return false for all elements in a collection. The some function is a bit different (notice there is no ?) and returns the first logical true value from the predicate function for the elements in the collection. So the return type of the predicate doesn’t have to be a Boolean value and then the return type of some is also not a Boolean. If the predicate returns a Boolean value we can use some like a `any` function (any is not part of Clojure). Clojure provides a not-any? function that returns true if the predicate function returns false for one element in the collection and false otherwise.

The following example uses the different functions on a vector with some cartoon names:

Continue reading →

Clojure Goodness: Get Clojure Version

Posted on by  
Hubert Klein Ikkink

To get the current Clojure version we must use the clojure-version function. The function simply returns the Clojure version we are using from our code.

In the following example we simply check the result of clojure-version and also define a function to get the Javaa version:

Continue reading →

Clojure Goodness: Splitting Strings

Posted on by  
Hubert Klein Ikkink

In Clojure we can use the clojure.string/split function to split a string, based on a regular expression, into a vector with string values. Optionally we can also specify a limit on the maximum number of returned string values we want. If we want to split a string based on the newline characters we can use the function clojure.string/split-lines that returns a vector where each element is a line from the original multi-line string.

The following example shows several usages of the split and split-lines functions:

Continue reading →

Clojure Goodness: Get Random Item From A Sequence

Posted on by  
Hubert Klein Ikkink

In Clojure we can use the rand-nth function to get a single random element from a sequence. To get multiple items based on random probability for each item we use the function random-sample. We must set the probability that determines for each item if it is in the result or not.

In the following example code we use rand-nth function:

Continue reading →

shadow-left