Posts by Hubert Klein Ikkink

Java Joy: Infinite Stream Of Values Or Method Invocations

Posted on by  
Hubert Klein Ikkink

In Java we can use the generate method of the Stream class to create an infinite stream of values. The values are coming from a Supplier instance we pass as argument to the generate method. The Supplier instance usually will be a lambda expression. To give back a fixed value we simply implement a Supplier that returns the value. We can also have different values when we use a method that returns a different value on each invocation, for example the randomUUID method of the UUID class. When we use such a method we can create the Supplier as method reference: UUID::randomUUID.

The generate method returns an unbounded stream. We must use methods like limit and takeWhile to get a bounded stream again. We must use findFirst or findAny to terminate the unbounded stream and get a value.

Continue reading →

Clojure Goodness: Transforming Collection Items With Index

Posted on by  
Hubert Klein Ikkink

If we want to transform items in a collection we can use the map function. If we also want to use the index of the element in the collection in the transformation we must use the map-indexed function. We must provide a function with 2 arguments, where the first argument is the index of the element in the collection and the second argument is the element in the collection.

In the following examples we use the map-indexed function:

Continue reading →

Clojure Goodness: Repeating A Value Or Function Invocation

Posted on by  
Hubert Klein Ikkink

In Clojure we can use the repeat function to get an infinite sequence of a given value. We can pass a length argument to get a fixed size sequence of the value. Clojure also provides the repeatedly function that takes as argument a function without arguments. A infinite sequence of invocations of the function is returned. Just like with the repeat function we can pass a length argument so the returned sequence has a fixed size.

We use the repeat and repeatedly function in the following example:

Continue reading →

Clojure Goodness: Interleave Keys And Values Into A Map With zipmap

Posted on by  
Hubert Klein Ikkink

The Clojure function zipmap create a map by interleaving a collection of keys with a collection of values. The first element of the keys collection is the map entry keyword and the first element of the values collection is than the map entry value, and so on for the other elements in the keys and values collections.

In the following example code we use zipmap using different examples of keys and values collections:

Continue reading →

Clojure Goodness: Composing Functions With comp

Posted on by  
Hubert Klein Ikkink

In Clojure we can use the comp function to create a new function based on multiple other functions. We create a new function by composing other functions. We can invoke the function that is returned from the comp function with input arguments. These arguments are applied to the right most function that was used with comp. Then the output of that function is the input for the next function.

In the following example code we see several usages of the comp function. Also we see how the ordening of the functions can change the output:

Continue reading →

Clojure Goodness: Using The range Function

Posted on by  
Hubert Klein Ikkink

In Clojure we can use the range function to create a lazy sequence of numbers. We can optionally specify a start value, end value and define the steps between the numbers. If we use the end value argument that value is exclusive for the returned values in the lazy sequence.

In the following example we invoke the range function with different arguments:

Continue reading →

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 →

Awesome Asciidoctor: Image As Link Reference

Posted on by  
Hubert Klein Ikkink

To make an image linkable in Asciidoctor when formatted to HTML we must use the link attribute when we use the image macro. The value of the link attribute is the address where the user goes when clicking on the image. We can also specify extra link attributes like window to specify the target window for the link to open in.

In the following example we use the link attribute for a block and inline image, with and without an extra window attribute:

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 →

shadow-left