DataWeave lambdas for Java programmers

dataweave howto

In Mule 4, DataWeave is everywhere: every listener and processor can be configured with it. Because most Mule users already know Java well, this article will help Java developers to easily use DataWeave by rewriting their lambdas expressions.

First, I will show why lambdas were created and explain the historical context. Next, I’ll explain the important point of when to use lambdas and when to not use lambdas. To conclude, I’ll show where you can use lambdas in your DataWeave code.

Why lambdas

Lambdas were first introduced with the Java 8 release in order to simplify the Java code. We will compare: 

  • the Java code without lambdas
  • the Java code with lambdas
  • the DataWeave 2.0 with lambdas

We will work with the following use case: “We want to order a list of Java Objects given criteria.”

Let’s first define a simple class for the objects we want to order:

Now we can create samples to work within both Java and DataWeave.

Now let’s create the code to sort our array of objects by age. We can compare the following three ways: Java without lambdas, Java with lambdas, and DataWeave with lambdas.

Java without lambdasJava with lambdasDataWeave with lambdas

Please notice that we only need the Comparator class in Java code when not using lambdas.

To summarize, let’s the code length by counting the number of characters in each case:

  • 305: the Java code without lambdas
  • 84: the Java code with lambdas
  • 41: the DataWeave 2.0 with lambdas

I think it is safe to conclude that lambdas minimize code length substantially.

When to use lambdas

In the previous section, we saw how lambdas simplify our code. Without lambdas, we had to create a Java utility class named CustomerComparator. That class implemented the Comparator interface and is only composed of one method. This is when we can use a lambda in Java.

Additionally, we might consider using a lambda in these cases:

  • When the code is trivial
  • When we don’t want to reuse the code in another location — a lambda is also called an “anonymous function”

A typical use case for lambdas is to execute an action when a button is pressed in a GUI (Graphical User Interface). In effect, the code attached to a button in a GUI has a low chance of re-use.

In DataWeave the more important reason for when to use a lambda expression is that we don’t want to reuse the code. Below is an anti-pattern followed by the correct way to write it:

Anti-pattern (give a name to a lambda)Best practice (create a function)

Anti-pattern use of lambdas due to reuse, in this case, the best practice is to create a function.

Where to use lambdas

The response to “where” to use lambdas is quite simple:

  • Use a lambda as a parameter to a method/function when we don’t want to reuse code

Of course, the method/function must accept a lambda as argument.

The following table shows where it is allowed to use a lambda in functions from the DataWeave’s Core package (dw::Core).

++filterObjectisIntegermaxrandomsqrtwrite
findisLeapYearmaxByrandomIntstartsWithzip
absflatMapisOddminreadsum
avgflattenjoinByminByreadUrlto
ceilfloorlogmodreducetrim
containsgroupBylowernativereplacetypeOf
daysBetweenisBlankmapnowroundunzip
distinctByisDecimalmapObjectorderByscanupper
endsWithisEmptymatchplucksizeOfuuid
filterisEvenmatchespowsplitBywith

Conclusion

In this article, I gave an overview of why, when, and where to use lambdas in DataWeave. If you want to learn more about DataWeave 2.0, you can register for the DataWeave course.



We'd love to hear your opinion on this post