When you write an arithmetic expression such as B * C, the form of the expression provides you with information so that you can interpret it correctly. In this case we know that the variable B is being multiplied by the variable C since the multiplication operator * appears between them in the expression. This type of notation is referred to as infix since the operator is in between the two operands that it is working on.
So far, we’ve been calling filter using prefix notation. With prefix notation you put the function name before the arguments. If a function takes two arguments, like filter does, DataWeave allows you to call it with infix notation. Infix notation has the following syntax:
<arg1> <function_name> <arg2>
This syntax is preferred for nearly every function that takes a lambda as its second argument. Here’s how the code from the previous section would look if we called filter using infix notation:
%dw 2.0 output json var numbers = (1 to 5) --- numbers filter ((n, idx) -> (n mod 2) == 1)
This may not seem like a great advantage, but it allows you to easily chain together functions that take in and return the same data type. For example, we can string together two filter functions in a way that is easy to read and understand:
%dw 2.0 output json var numbers = (1 to 5) --- numbers filter ((n, idx) -> (n mod 2) == 1) filter ((n, idx) -> (n > 3))
In this case, the Array is filtered on whether or not the item is odd, then the result of that filter is filtered on whether or not the number is greater than 3.
Notice the additional parentheses around the first lambda. The parenthesis around the lambdas help DataWeave determine where the lambda starts and ends.
If you like my post please follow me to read my latest post on programming and technology.