Skip to main content

pairwiseMap

Pairs values from two lists, maps each pair to a single result, and returns a new list.

The first and second parameters take lists or list-like objects (e.g. columns). The function pairs corresponding values from these inputs. For example, the first value in LIST 1 is paired with the first value in LIST 2.

The third parameter defines a function that maps each pair of values to a single value. You must write the function as a lambda expression. The examples below provide further guidance.

Parameters

  • LIST 1(list, column, row, or array)

    The first list or list-like object.

  • LIST 2 (list, column, row, or array)

    The second list or list-like object.

  • FUNCTION (function)

    An anonymous function that returns a single value for each input pair. Write the function as lambda expression and ensure it has two parameters — e.g. (value1, value2) -> sum(value1, value2).

Examples

ATL in Script

Resulting List

Printed Result

[[

list1 = makeList(1, 2, 3)

list2 = makeList(4, 5, 6)

pairwiseMap(list1, list2, (x,y) -> sum(x,y))

]]

(5, 7, 9)

5, 7 and 9

First, the function pairs corresponding values from list1 and list2 to create a list of pairs. Next, it maps each pair to a single value by applying the lambda expression (x,y) -> sum(x,y). This tells pairwiseMap to sum the numbers in each pair and include the result in the output list.

Using pairwiseMap with table data

Assume a "Describe the Table" project with this data:

Code

Product

Sales

Target

Row 1

1001

Retine

198248.36

200000

Row 2

1002

Ointmo

267321.77

250000

Row 3

1003

Glower

182434.58

200000

A column variable is a list-like object and therefore a valid input to the pairwiseMap function.

Here's how to pair corresponding values from two columns, then map each pair to a piece of narrative text:

[[pairwiseMap(Product, Sales, (x,y) -> "[[x]] sales were [[abbreviateNumber(currencyFormat(y,'GBP'))]]")]].

The output text is:

Retine sales were £198.2K, Ointmo sales were £267.3K and Glower sales were £182.4K.

Note

In a "Describe Row in Context" project, the column variables are ProductColumn and SalesColumn.

Using pairwiseMap with JSON data

Assume a "Describe a JSON Object" project with this data:

{
    "sales": {
        "Q1data": [
            {"product": "Retine", "value": 2493146.89},
            {"product": "Ointmo", "value": 3486198.56},
            {"product": "Glower", "value": 1756934.95}
        ],
        "Q2data": [
            {"product": "Retine", "value": 2788146.22},
            {"product": "Ointmo", "value": 3027634.12},
            {"product": "Glower", "value": 1831749.94}
        ]
    }
}

The "Q1data" and "Q2data" arrays each contain three JSON objects, and each object contains two key–value pairs. With this dataset, you might want to analyze how product sales have changed from Q1 to Q2.

Arrays are list-like objects, so "Q1data" and "Q2data" are valid inputs to pairwiseMap.

You could use pairwiseMap with createAtlObject to produce a list of ATL objects.

[[

Q1array = WholeJSON.sales.Q1data

Q2array = WholeJSON.sales.Q2data

pairwiseMap(Q1array, Q2array, (x,y) -> createAtlObject(('product', 'changePct', 'description'), (x.product, percentageChange(y.value, x.value), y.value > x.value ? 'rose' : 'fell')))

]]

The result is this list of ATL objects:

( (product = Retine, changePct = 11.8, description = rose), (product = Ointmo, changePct = -13.2, description = fell), (product = Glower, changePct = 4.3, description = rose) )

Here's how you might use the result to produce narrative text:

[[

Q1array = WholeJSON.sales.Q1data

Q2array = WholeJSON.sales.Q2data

resultsObject = pairwiseMap(Q1array, Q2array, (x,y) -> createAtlObject(('product', 'changePct', 'description'), (x.product, percentageChange(y.value, x.value), y.value > x.value ? 'rose' : 'fell')))

"From Qtr 1 to Qtr 2, [[map(resultsObject, x -> joinStrings(x.product, ' sales ', x.description , ' by ', abs(x.changePct),'%'))]]."

]]

The output text is:

From Qtr 1 to Qtr 2, Retine sales rose by 11.8%, Ointmo sales fell by 13.2% and Glower sales rose by 4.3%.