"It’s official! In April I will be starting an amazing new job!", I thought excitedly as I laid down my pen.
I had just signed my contract with JCore during a nice lunch with a soon-to-be colleague.
It was December 23st and signing the contract felt like an early Christmas present.
Not only would JCore offer me plenty of opportunity to develop my technical and personal skills, they also offered a fun social environment.
During the interviews I was told about pub quizzes, board game nights, Friday afternoon drinks, people playing videogames together…
It seemed so much fun!
I joined two of these events even before I officially started working for JCore.
I had a great time and I was really looking forward for this to become my new normal.
Little did I know that my actual new normal would be vastly different due to the corona crisis.
Since beginning of time mankind has been looking for a way to separate right from wrong. Where the primeval man judged righteousness by the contributions of the tribe, the current day programmer judges right by the wishes of the customer. For many years the average programmer wrote a bunch of logic to check if the boundaries defined by the client where uphold. As time went on and programming languages involved, metadata could be added to enrich functions, methods, classes and the like.
Of course for Java, these metadata are called annotations. Very soon they were used for a lot of things. Surpressing warnings, managing transactions, building XML/JSON structures and injecting dependencies. And, as you might have guessed by now, validating objects by a set of specific rules. One of the most commonly used frameworks would be the Jakarta Bean Validation framework. But what if I told you the provided annotations of that framework could be very easily expanded.
Continue reading →
When we are working with sets in Clojure we can use some useful functions from the clojure.set
namespace. In a previous post we learned how we can get the difference of several sets. To get the union of several input sets we use the union
function of the clojure.set
namespace. The function returns a new set that is the union of unique elements from the input sets. A nil
value is ignored by the union
function.
In the following example code we use union
:
Continue reading →
If we want to get the values from a set that are not part of one or more other sets we must use the difference
function in the clojure.set
namespace. The function returns a set with all values from the first set that are different from values in other sets.
In the following example we use the difference
with several sets:
Continue reading →
In Clojure functions are everywhere. In a previous post we learned that sets can be functions, but Clojure also makes keywords functions. A keyword is a symbol starting with a colon (:
) and is mostly used in map entries as key symbol. The keyword as function accepts a map as single argument and returns the value for the key that equals the keyword in the map or nil
if the keyword cannot be found.
In the following code we use keywords as function in several examples:
Continue reading →
One of the nice things in Clojure is that some data structures are also functions. For me this felt rather strange when learning Clojure (coming from Java), but it can be very powerful. A set in Clojure is also a function. The set as function accept a single argument and it return nil
when the argument is not part of the set, otherwise the argument value is returned. This behaviour also makes a set as function a nice predicate to be used for example in collection functions.
In the following example code we use different sets as function:
Continue reading →
The Clojure function complement
can be used to created a new function that returns the opposite truth value of the old function. The new function accepts the same number of arguments as the old function. Also when we invoke the new function created by the complement
the old function is actually invoked and the result is used as argument for the not
function to return the opposite truth value. So if the original function returns false
or nil
the result for the new function is true
.
In the following example code we create a new function bad-weather
that is the complement of good-weather
:
Continue reading →
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 →