Infix Notation
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)
[1,3,5]
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))
[5]
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.
You are given a stream of elements that is too large to fit into memory.…
The formula for the area of a circle is given by πr². Use the Monte…
Given an integer k and a string s, write a function to determine the length…
There is a staircase with N steps, and you can ascend either 1 step or…
Build an autocomplete system that, given a query string s and a set of possible…
Design a job scheduler that accepts a function f and an integer n. The scheduler…