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

Generate Parenthesis | Intuition + Code | Recursion Tree | Backtracking | Java

Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…

3 months ago

Square Root of Integer

Given an integer A. Compute and return the square root of A. If A is…

1 year ago

Build Array From Permutation

Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…

1 year ago

DSA: Heap

A heap is a specialized tree-based data structure that satisfies the heap property. It is…

2 years ago

DSA: Trie

What is a Trie in DSA? A trie, often known as a prefix tree, is…

2 years ago

Trees: Lowest Common Ancestor

What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…

2 years ago