Infix Notation

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 Script:

%dw 2.0
output json

var numbers = (1 to 5)
---
numbers filter ((n, idx) -> (n mod 2) == 1)

Output:

[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 Script:

%dw 2.0
output json

var numbers = (1 to 5)
---
numbers
  filter ((n, idx) -> (n mod 2) == 1)
  filter ((n, idx) -> (n > 3))

Output:

[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.

Follow Me

If you like my post please follow me to read my latest post on programming and technology.

Instagram

Facebook

Recent Posts

Minimum Cost to Paint Houses with K Colors

A builder plans to construct N houses in a row, where each house can be…

1 day ago

Longest Absolute Path in File System Representation

Find the length of the longest absolute path to a file within the abstracted file…

3 weeks ago

Efficient Order Log Storage

You manage an e-commerce website and need to keep track of the last N order…

1 month ago

Select a Random Element from a Stream

You are given a stream of elements that is too large to fit into memory.…

1 month ago

Estimate π Using Monte Carlo Method

The formula for the area of a circle is given by πr². Use the Monte…

2 months ago

Longest Substring with K Distinct Characters

Given an integer k and a string s, write a function to determine the length…

2 months ago