Advanced array functions

DecisionRules is constantly improving its collection of advanced functions which can be used for custom computation within decision tables and decision trees. In this article, we shall look at a specific and very useful type of functions treating arrays.

Jakub Kaninsky
Desarrollador Líder
?
Infórmate
Check icon A checkmark inside a circle signifying "yes" Minus icon A minus inside a circle signifying "no" PROS Icon A plus symbol representing positive aspects or benefits. CONS Icon A minus symbol representing negative aspects or drawbacks.

Array is an ordered set of elements – numeric values, strings, or more complex JSON objects. When calling a decision table, we often include arrays in the input data, e.g. to specify a list of available delivery methods, client profiles or items in a shopping cart. We can then refer to the array within the rule conditions or results and base our decision logic on its data. This is where array functions come in handy.

Let us stay with the example of items in a shopping cart, and suppose that we have a simple decision table in which we want to work with the total price of the order. In a simplified case, the input of our decision table may look as follows.

Now, imagine you want to apply a discount based on the total price of the items present in the cart. You can definitely do that, but you first need to sum up the prices. This is where you can take advantage of an array function. In the respective cell within the column for the cart input variable, you choose the function cell type and enter the following function.

GT(SUM(ARRAY_PICK({cart}, "price")), 35)

Let us see what it means. The ARRAY_PICK function obtains the array sent over in the cart input variable, and returns an array of values under the chosen price key. For example, the input provided above will result in the array [12,28]. This array of prices is then passed to the SUM function which computes the total price of the items in the cart. With the input above, we will get the total price equal to 40. The GT function then simply checks whether the total price is greater than 35. In the case considered, the result will be true, and the evaluation of the respective table row will continue (we can assign the discount, for instance).

It is now clear that array functions are really useful. Yet they can do much more than just summing values nested inside an array of objects. To see the full capability of advanced array functions, let us present the three most powerful ones.

Array Map

A common task is to map one array to another. This is what ARRAY_MAP is for. Let us assume you have a decision table with the following input data.

Now imagine you want to increase each of the numbers in the inputArray array by one. You can do that easily with ARRAY_MAP. Just write the following code.

ARRAY_MAP({inputArray},"number",{number}+1)

The ARRAY_MAP function takes three arguments: the array to map, a name of a custom argument and a custom function of this argument. Here, we have named the custom argument "number" and the custom function is just {number}+1 which increases the number by one. The ARRAY_MAP function takes each element of the inputArray, puts it to {number} and evaluates the {number}+1 function. The result is then passed to the resulting array. Thus, the result of this example will be the array of values [2,3,4].

Array Reduce

What if we do not want to map an array to another array, but to a single value? For most cases, this can be done with ARRAY_REDUCE. Let us keep the input data from the preceding example:

Let us try to compute the product of all the numbers inside the array. With ARRAY_REDUCE, this can be achieved instantly.

ARRAY_REDUCE({inputArray},"a","b",{a}*{b})

Note that ARRAY_REDUCE needs to be given the names of two custom arguments – we have named them "a" and "b" – and a custom function of the two arguments. For us, the custom function is simply the multiplication {a}*{b}. ARRAY_REDUCE goes through the given array and applies the custom function successively. First, it takes {a} = 1 and {b} = 2 and gets {a}*{b} = 2. Then, it takes {a} = 2 from the preceding calculation and {b} = 3, thus getting {a}*{b} = 6. At this point, the computation ends, since our inputArray has only three elements. The result is 6, as expected.

Array Filter

The last array function we want to introduce here is ARRAY_FILTER. Not surprisingly, it filters a given array according to a custom filter function. We can once again consider the same input data:

Assume that we want to keep only those values that are greater than 1. This is a perfect task for ARRAY_FILTER. Just write the following.

ARRAY_FILTER({inputArray},"number",GT({number},1))

Here, the ARRAY_FILTER function looks at every element of the inputArray, passes it as {number} to the custom function GT({number},1) and checks whether the result is true. If it is the case, the element is passed to the resulting array, otherwise not. The resulting array will therefore be [2,3] since the first number did not pass the filter.

Conclusion

Array functions are a powerful tool for working with arrays in decision tables and decision trees. The example with items in a shopping cart demonstrates the basic usage. Besides ARRAY_SUM, there are other similar functions for treating strings or boolean values. Moreover, there are three special array functions offering great freedom thanks to the possibility of passing other functions to them as arguments. These allow us to map arrays to other arrays with a custom mapping, reduce arrays based on a custom reducer or filter arrays according to custom filter. Altogether, the array functions form a versatile system for treating arrays, covering most common use cases.

For more details, see Array Functions in our docs.

Thanks for reading!

Enjoyed the read? Let’s take the next step together.