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 →
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 →
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 →
Since Java 9 we can use a function as argument for the Matcher.replaceAll
method. The function is invoked with a single argument of type MatchResult
and must return a String
value. The MatchResult
object contains a found match we can get using the group
method. If there are capturing groups in the regular expression used for replacing a value we can use group
method with the capturing group index as argument.
In the following example we use the replaceAll
method and we use a regular expression without and with capturing groups:
Continue reading →
In Java we can define capturing groups in regular expression. We can refer to these groups (if found) by the index from the group as defined in the regular expression. Instead of relying on the index of the group we can give a capturing group a name and use that name to reference the group. The format of the group name is ?<name>
as first element of the group definition. The name of the group can be used with the group
method of the Matcher
class. Also we can use the name when we want to reference the capturing group for example with the replaceAll
method of a Matcher
object. The format is ${name}
to reference the group by name. Finally we can use a named capturing group also as backreference in a regular expression using the syntax \k<name>
.
In the following example we define a regular expression with named groups and use them with several methods:
Continue reading →
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 →
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 →
The keep
function in Clojure invokes a function on each item in a collection and only returns non-nil results from the function invocation. The result of the keep
function is a lazy sequence.
The following example uses the keep
function, but also show what results would be when using map
function on the same collection with the same function argument:
Continue reading →
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 →
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 →
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 →
We can use the flatten
function when we have a collection with nested sequential collections as elements and create a new sequence with the elements from all nested collections.
In the following example we use the flatten
function:
Continue reading →