In Java we can use the iterate
method of the Stream
class to create an unbounded stream based on function invocations. We pass to the iterate
method an initial value and a function that can be applied to the value. The first element in the unbounded stream is the initial value, the next element is the result of the function invocation with as argument the value from the previous element and this continues for each new element. Suppose we have a function expressed as lambda expression i → i + 2
. When we use this lambda expression with the iterate
method and a initial value of 1
we get a stream of 1
, 1 → 1 + 2
, 3 → 3 + 2
, ….
As we get an unbounded stream we must for example use limit
to get the values we want from the stream. But we can also use an extra argument for the iterate
method that is a Predicate
definition. The iterate
method will provide elements as long as the result of the Predicate
is true
. This way we the result of the iterate
method is a bounded stream.
Continue reading →
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 →
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 →
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 →
The Clojure function cycle
take a collections as argument and creates a lazy sequence by repeating the items in the collection. So if we pass a collection with the characters \a
, \b
and \c
we get a lazy sequence of (\a \b \c \a \b \c …)
.
Written with Clojure 1.10.1.
Continue reading →
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 →
How do you know when having a tech lead may be taking your organisation in the wrong direction?
Let’s take a look at some of the archetypical tech leads, their pitfalls and ways to go about and deal with them.
Continue reading →
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 →
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 →
At the time of writing the coronavirus is raging the earth. Very soon after the outbreak, visualizations of both the virus and the effect of the disease started to appear everywhere. As I partially graduated in the subject of data visualization, I have always been interested in those graphs. Lately, I followed an introduction course to visualize data with D3.js. After I completed this course, I wanted to draw some meaningful graphics with this library. So follow along when I explain a little bit about D3 and then draw a simplified version of the coronavirus molecule.
Continue reading →