Clojure Goodness: Destructure Sequences

Clojure supports advanced destructure features. In a previous post, we learned about destructuring maps, but we can also destructure vectors, lists, and sequences in Clojure using positional destructuring. We can define symbols for positions in the sequence to assign the value at a certain position to the symbol. The first symbol in the destructure vector gets the value of the first element in the sequence, the second symbol the value of the second element, and so on. To get the remaining elements from the sequence without assigning them to specific symbols we can use & followed by a symbol. Then all remaining elements are assigned as sequence the symbol. Finally, we can use :as to get the original vector, list, or sequence.

The following examples show several destructure definitions for different types of collections and sequences:

Java Joy: Turn Streams Into Arrays [Snippet]

The Java Stream API has many useful methods. If we want to transform a Stream into a Java array, we can use the toArray method. Without an argument, the result is an object array ( Object[]), but we can also use an argument to return an array of another type. The easiest way is to use the contructor of the array type we want as a method reference. Then, the result is an array of the given type with the elements of the stream.

This is very useful if we have a Java Stream and want to use the elements to invoke a method with a variable arguments parameter. In Java, we can pass an array object as variable arguments argument to a method. So if we transform the Stream into an array we can invoke the method with that value.

Gradle Goodness: Replace Files In Archives

Sometimes, we might need to replace one or more files in an existing archive file. The archive file could be a zip, jar, war, or another archive. Without Gradle, we would unpack the archive file, copy our new file into the destination directory of the unpacked archive, and archive the directory again. To achieve this with Gradle, we can simply create a single task of type Zip

To get the content of the original archive, we can use the project.zipTree method. We leave out the file we want to replace and define the new file as a replacement. As an extra safeguard, we can let the task fail if duplicate files are in the archive because of our replacement.

Java Joy: Combining Predicates

Combining predicates in Java brings developers lots of joy.

In Java, we can use a Predicate to test if something is true or false. This is especially useful when we use the filter method of the Java Stream API.

You may also like: Towards More Functional Java Using Lambdas as Predicates

We can use lambda expressions to define our Predicate or implement the Predicate interface. If we want to combine different Predicate objects, we can use the or, and, and negate methods of the Predicate interfaces. These are default methods of the interface and will return a new Predicate.

Awesome Asciidoctor: Don’t Wrap Lines in All Listing or Literal Blocks of Document

In a previous post, we learned about setting the value of options attribute to nowrap on listing and literal blocks, so the lines in the block are not wrapped. In the comments, a user suggested another option to disable line wrapping for all listing and literal blocks in the document by using the document attribute prewrap. We must negate the document attribute, :prewrap!:, to disable all wrapping. If we place this document attribute at the top of our Asciidoctor document, it is applied for the whole document. We can also place it at other places in the document to change the setting for all listing and literal blocks following the prewrap document attribute. To enable wrapping again, we set :prewrap: (leaving out the exclamation mark).

In the following example, we have markup with a listing, literal, and example block, and we use the document attribute :prewrap!: to disable the wrapping for the listing and literal blocks:

Micronaut Mastery: Change the Default Package for Generated Classes

When we use the Micronaut command line commands to generate controllers, beans, and more, the classes will have a default package name. We can use a fully qualified package name for the class we want to generate, but when we only define a class name, Micronaut will use a default package name automatically. We can set the default package for an application when we first create an application with the create-app command. But we can also alter the default package name for an existing Micronaut application.

To set the default package name when we create a new application we must include the package name in our application name. For example when we want to use the default package name mrhaki.micronaut for an application that is called book-service we must use the following create-app command:

Spring Sweets: Group Loggers With Logical Name [Snippet]

Spring Boot 2.1 introduced log groups. A log group is a logical name for one or more loggers. We can define log groups in our application configuration. Then, we can set the log level for a group, so all loggers in the group will get the same log level. This can be very useful to change a log level for multiple loggers that belong together with one setting. Spring Boot already provides two log groups by default: web and sql. In the following list, we see which loggers are part of the default log groups:

  • web: org.springframework.core.codec, org.springframework.http, org.springframework.web, org.springframework.boot.actuate.endpoint.web, org.springframework.boot.web.servlet.ServletContextInitializerBeans
  • sql: org.springframework.jdbc.core, org.hibernate.SQL

To define our own log group, we must add in our application configuration the key logging.group. followed by our log group name. Next, we assign all loggers that we want to be part of the group. Once we have defined our group, we can set the log level using the group name prefixed with the configuration key logging.level..

Gradle Goodness: Only Show All Tasks in a Group [Snippet]

To get an overview of all Gradle tasks in our project, we need to run the tasks task. Since Gradle 5.1, we can use the --group option followed by a group name. Gradle will then show all tasks belonging to the group and not the other tasks in the project.

Suppose we have a Gradle Java project and want to show the tasks that belong to the build group: