Sample blogtober 2020
First paragraph.
My post with code
First paragraph.
My post with code
In Clojure we can take or drop elements from a collection based on a predicate using the functions take-while
and drop-while
. With the function take-while
we take elements as long as the predicate returns true
. Once the predicate returns false
the function stops returning elements. Using the function drop-while
we skip elements in the collection if the predicate returns true
. If the predicate returns false
the remaining elements in the collection are returned.
In the following example we use take-while
and drop-while
with different collection types:
The map data structure is used a lot in Clojure. When we want to use Java objects in our Clojure code we can convert the Java object to a map with the bean
function. This function will use reflection to get all the properties of the Java object and converts each property with the property value to a key with value in the resulting map. The bean
function will not recursively convert nested objects to a map, only the top-level properties are turned into key value pairs.
We see several examples of using the bean
function in the following code snippet:
It is very easy to work with Java classes in Clojure. If we want to create a new object based on a Java class and invoke methods to initialize the object directly we can use the doto
macro. The first argument is an expression to create a new object and the rest of the arguments are functions to invoke methods on the newly created object. The object returned from the first argument is passed as first argument to the method invocations. The doto
function returns the object that is created with the first argument.
In the following example code we use the doto
function in several cases:
The clojure.string
namespace contains a lot of useful functions to work with string values. The escape
function can be used to replace characters in a string with another character. The function accepts as first argument the string value and the second argument is a map. The map has characters as key that need to be replaced followed by the value it is replaced with. For example the map {\a 1 \b 2}
replaces the character a
with 1
and the character b
with 2
.
In the following example code we use the escape
function in several cases:
When we use a function as argument for the map
function that returns a collection we would get nested collections. If we want to turn the result into a single collection we can concatenate the elements from the collections by applying the concat
function, but we can do this directly with the function mapcat
. The function mapcat
takes as first argument a function (that returns a collection) and one or more collections as next arguments.
In the following examples we see several uses of mapcat
:
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
:
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:
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:
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:
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
:
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.