Skip to main content

Lambda expressions

A lambda expression is an anonymous function — that is, a function without a name. In ATL, you may write a lambda expression when using a higher-order function such as map, filter, or sort.

A higher-order function is one that takes another function as a parameter. In ATL, higher-order functions operate on lists or list-like structures such as JSON arrays. Typically, these functions take a list, apply a function to each value, then return an amended list. You can give the applied function — the one applied to each list value — as a lambda expression. Here's a simple example:

ATL in Script

Input List

Amended List

Printed Result

[[

myList = makeList(10, 20, 30);

map(myList, x -> x + 1)

]]

(10, 20, 30)

(11, 21, 31)

11, 21 and 31

The map function iterates through a list of numbers (myList), applying a function (x -> x + 1) to each number. The effect is to increase each list value by 1. The applied function — the one that modifies each list value — is a lambda expression.

We have structured the rest of this topic as follows:

Syntax for lambda expressions

The basic syntax for a lambda expression is:

parameter -> functionBody

Element

Description

parameter

Defines the function's parameter

->

The lambda operator

functionBody

Defines what the function does

For example:

Lambda Expression

Explanation

y -> y * 10

There is one parameter, represented by y. The function body (y -> y * 10) tells Studio to take the value for y and multiply it by 10.

For multiple parameters, the syntax is:

(parameter1, parameter2, parameter3) -> functionBody

For example:

Lambda Expression

Explanation

(a, b, c, d) -> (a + b + c) / d

There are four parameters, represented by a, b, c, and d. The function body tells Studio to sum the values for a, b, and c and then divide by d.

Tip

You don't need to use single characters to represent parameters. For example, (para1, para2) -> para1 + para2 performs the same operation as (x,y) -> x + y. Using single characters helps to keep your code concise.

Using ATL functions in lambda expressions

You can use ATL functions in your lambda expression. The first example below uses basic arithmetic operators to perform sum aggregation and division, but the second example shows you can use math functions such as sum and div instead:

Lambda Expression

Explanation

(a, b, c, d) -> (a + b + c) / d

There are four parameters, represented by a, b, c, and d. The function body tells Studio to sum the values for a, b, and c and then divide by d.

(a, b, c, d) -> div(sum(a,b,c),d)

There are four parameters, represented by a, b, c, and d. The function body tells Studio to sum the values for a, b, and c and then divide by d.

You can nest ATL functions within a lambda, using the usual syntax. This is shown in the second example above — note that a call to the sum function — sum(a,b,c) — forms the first parameter input to the div function.

Tip

For a full list of ATL functions, see Index of ATL functions.

You can also call a user-defined function within a lambda expression.

Lambda expressions and higher-order functions

You may write a lambda expression when using a higher-order function. A higher-order function is one that takes a function as a parameter. The higher-order functions in Studio are integral to retrieving and organizing data.

The most popular higher-order functions are map, filter, and sort. We've included examples for each below.

Mapping with lambda expressions

The map function iterates through a list, applies a function to each value, and returns a list of amended values. You can write the amending function as a lambda expression. For example:

ATL in Script

Printed Result

[[

myList = makeList(10, 20, 30);

map(myList, x -> numToWords(x))

]]

Ten, twenty and thirty

[[

myList = makeList(10, 20, 30);

map(myList, number -> joinStrings(number,"%"))

]]

10%, 20% and 30%

[[

myList = makeList(1520, 2760, 3728);

map(myList, a -> abbreviateNumber(currencyFormat(a,'GBP')))

]]

£1.5K, £2.8K and £3.7K

Tip

You can use the map function to map a more complex list-like structure — for example, an array of JSON objects — to a simple list of values. See the more complex examples in the function topic for guidance.

Filtering with lambda expressions

The filter function takes a list of values, applies a filter, and returns a list containing all values that pass the filter.

The input to the first parameter is a list or list-like object. The input to the second parameter is a function that defines the filter conditions. This must be a Boolean function — that is, one that returns a Boolean value of true or false.

The filter function iterates through a list, applying the Boolean function to each value. If the result of applying the Boolean function is true, the value passes the filter and appears in the output list. You can write this Boolean function as a lambda expression.

ATL in Script

Printed Result

[[

myList = makeList(10, 20, 30, 40, 50);

filter(myList, y -> y > 20 && y < 50)

]]

30 and 40

[[

myList = makeList('Andy','Linda','Louise','Chrissie');

filter(myList, value -> endsWith(value,'e'))

]]

Louise and Chrissie

[[

myList = makeList('Andy','Linda','Louise','Chrissie');

filter(myList, inputName -> len(inputName) < 6)

]]

Andy and Linda

Tip

You can use filter to filter a JSON array — see the function topic for guidance.

To filter a data table — for example, to get all rows for which the Quarter value is Q2 — use filterRows instead. This function can also take a lambda expression — see the function topic for examples.

Sorting with lambda expressions

The sort function sorts a list or list-like object such as a JSON array. The function performs the sort by applying a comparator function to the input data. You can write the comparator function as a lambda expression — see the function topic for guidance.

Here are some simple examples:

ATL in Script

Printed Result

[[

myList = makeList(30, 10, 40, 20, 50); 

sort(myList, (x,y) -> sign(x - y))

]]

10, 20, 30, 40 and 50

[[

myList = makeList(30, 10, 40, 20, 50);

sort(myList, (x,y) -> sign(y - x))

]]

50, 40, 30, 20 and 10

[[

myList = makeList('C','D','A','E','B');

sort(myList, (a,b) -> a > b ? 1 : (a < b ? -1 : 0))

]]

A, B, C, D and E

Note that Studio has other sorting functions, many of which don't require a lambda expression. For example, [[sortStrings(myList)]] is a simpler alternative to [[sort(myList, (a,b) -> a > b ? 1 : (a < b ? -1 : 0))]].

If you want to sort a data table by the values in one column — for example, to sort by the values in the Sales column — use a sortBy function such as sortByColumnNumbers or sortColumnNumbersReverse. These functions don't require a lambda.

Tip

For a full list of sorting functions, see the Advanced functions index.

Lambda expressions and conditionals

The function body of your lambda expression can be a conditional statement. The best syntax for this is ternary syntax, which is ideal for writing very compact if-else statements.

Here, the map function iterates through a list of numbers (myList), applying a conditional statement to each one:

ATL in Script

Printed Result

[[

myList = makeList(60.65, 105.42, 8.34);

map(myList, value -> value < 10 ? round(value,2) : round(value,1))

]]

60.7, 105.4 and 8.34

The lambda expression value -> value < 10 ? round(value,2) : round(value,1) starts by asking whether the value under evaluation is LESS THAN 10. If true, the value is rounded to two decimal places; else, the value is rounded to one decimal place.

Note

Studio applies half-up rounding by default.

You can nest if-else statements to create a more complex conditional statement, if required.

ATL in Script

Printed Result

[[

myList = makeList(60, 75, 24, 59);

map(myList, value -> value < 50 ? "FAIL" : (value < 60 ? "PASS" : (value < 75 ? "MERIT" : "DISTINCTION")))

]]

MERIT, DISTINCTION, FAIL and PASS

In the example above, the map function iterates through a list of numbers (myList) and returns a string value for each one. The string value varies depending on the size of the number. The logic of the conditional is:

  • if the value is LESS THAN 50, return "FAIL"; else

  • if the value is LESS THAN 60, return "PASS"; else

  • if the value is LESS THAN 75, return "MERIT; else

  • return "DISTINCTION".