Reactor

Working With Lambdas and Lambda Helper Functions

Mappings: Functions and Expressions · Updated June 9, 2025

This page describes how to use Lambda, Lambda Helper, and other advanced mapping functions to map complexly-structured data to one or more semantic labels in Reactor.

What are Lambda functions and Lambda Helper functions?

What is a Lambda?

A Lambda function is a way to create custom formulas and parameters directly within a mapping expression. They are especially useful when mapping a data source that includes nested arrays and objects. Lambda functions can be used to complete the following actions within a mapping expression:

  • Complex mathematical calculations that are not available through built-in functions (e.g., factorials, square roots, or trigonometric calculations).
  • Data manipulation that requires custom filters.
  • Custom aggregations like weighted averages.

The Lambda function is formatted as LAMBDA (expression, variable_name, [variable_name...]), where expression is the formula or expression you wish to calculate, and variable_name is the variable used in the expression.

Lambda functions are used in conjunction with a special class of functions called Lambda Helper Functions. These functions apply the logic of a Lambda within a mapping expression, because Lambda expressions do not result in data outputs by themselves. The "Lambda Helper Function" column of the mapping functions table (not included here, but typically found in our documentation) denotes whether a function is a Lambda Helper Function.

Please note: Any function can be used in conjunction with a Lambda function, not just Lambda Helper Functions. For example, you can wrap a Sum function around a Map and Lambda function to sum the values of all results returned by the Map function. For example, when mapping ecommerce order data in Reactor, a field like total_shipping_price can utilize such a nested function, since an order can contain more than one shipping charge.

Additionally, the word each can be used in a Lambda Helper expression in cases where a repeated object is referenced. So, in the total_shipping_price example above, each shipping charge can be referenced in a MAP function like this (which references the shipping_lines array in a Shopify order):

SUM(MAP(each.price, GET("shipping_lines",source,[])).

In this expression: 

  1. GET("shipping_lines",source,[]) is the Lambda expression, it retrieves each object in the source's shipping_lines array.
  2. MAP(each.price, GET("shipping_lines",source,[]) selects all of the price attributes in the shipping_lines array - this expression alone should output a list of numbers (like [4.99, 0.00]).
  3. The SUM() function adds all the numbers in the list output by the MAP()function.

Example Expressions Using Lambda Functions

For a simple example, the expression below describes a Lambda function with two variables:

LAMBDA(foo + bar, foo, bar)

To apply this Lambda, we can wrap it in the (aptly-named) APPLY function and name our variables, like so:

  • APPLY(LAMBDA(foo + bar, foo, bar),"Hello, ", "Steve"): This expression outputs "Hello, Steve".
  • APPLY(LAMBDA(foo + bar, foo, bar),source.first_name, source.last_name): This expression outputs the values of the first_name and last_name fields in source data, with no spaces or other separating characters.
  • APPLY(LAMBDA(foo + bar, foo, bar),1,2): This expression outputs the number 3 (i.e., the sum of 1 and 2).

A more concrete example mapping expression that uses a LAMBDA is below. This expression assumes that the source data includes the following attributes:

  • wholesale_price: Equal to the price the retailer paid for an item.
  • markup_percentage: Equal to the percentage markup applied by the retailer to determine the retail price, a number between 0 and 100.

In this example, the price of the item is equal to wholesale_price * (1 + (markup_percentage / 100)), rounded to the nearest cent.

To express this calculation using a Lambda function, we could write the expression below:
ROUND(APPLY(LAMBDA(a*(1+(b/100)), a, b), DECIMAL(source.wholesale_price), DECIMAL(source.markup_percentage)),2)

 

Lambda Tutorial: Reducing Repeated Attributes in an Object or Array into One Value

Basic Example: Summing Repeated Metrics in an Array

For this example, consider a data source that includes all order total components within one object, formatted like this:

{
"totals": [
{"id": "Subtotal", "value": 52.9},
{"id": "Shipping", "value": 0},
{"id": "Discount", "value": -5},
{"id": "Tax", "value": 5.79}
]
}

The order total can be derived by summing each value in the object. To complete this operation in Reactor, you must write a mapping expression that does all of the following activities:

  1. Extracts each value from the object.
  2. Sums the values extracted in the first step.

This can be done using the following functions in concert:

  • Lambda: A function to name variables used in the mapping expression.
  • Map: A Lambda helper function that returns all values of a specific variable (named in the Lambda function).
  • Get: A function to retrieve the value of an attribute within an object (the source data, in this case).
  • Sum: A function that sums the values returned by the MAP function.

The first step is to name the object from which we are extracting values, using a GET function:

GET("totals",source,{})

This part of the function returns the totals object from the source data (see above), or an empty object ({}) if no totals object exists.

Next, a MAP function (nesting another GET function) and a Lambda are added to extract the value fields from each sub-object in the totals object:

MAP(GET("value", each, 0),GET("totals",source,{}))

In this example, each is the Lambda (because we want to retrieve each value in the totals object). The expression returns a list values, like this:

[52.9, 0, -5, 5.79]

Lastly, a SUM function is wrapped around the entire function, so that all of the values in the MAP function are added together to return a single result:

SUM(MAP(GET("value", each, 0),GET("totals",source,{})))

This returns 53.69.

Advanced Example 1: Adding an IF Function to Evaluate Values in a List for Inclusion in a SUM Function

But what if we only want to sum certain values in the object based on the id field? For this example, consider a more complex totals object that includes several Tax amounts, with the objective of mapping the total_tax field equal to the sum of all tax amounts:

{
"totals": [
{"id": "Tax1", "value": 1.00},
{"id": "ShippingTax", "value": 0.50},
{"id": "CustomTax", "value": 3.18},
{"id": "OtherTax", "value": 0.13},
{"id": "CustomTax", "value": 1.19},
{"id": "VAT", "value": 0.53}
]
}

In this example, any value whose id is "CustomTax" should be included in the calculation for total tax. An IF function can be used to evaluate whether a value should be included, with the following logic:

  • If id = "CustomTax": Include the value.
  • If id does not = "CustomTax": Do not include the value, and use 0 instead.

The IF function will also need a Lambda, MAP, and GET function in order to evaluate the condition specified above. The entire mapping expression looks like this:

SUM(
MAP(
IF(
GET("id", each, null) == "CustomTax",
GET("value",each,0),
0),
GET("totals",source,[])
)
)

The expression does the following:

  1. Gets the totals array from the source object (GET("totals",source,{})), or returns an empty array if no totals array exists.
  2. In each totals object, returns the value of the value field if the value of the id field in that object is "CustomTax"; otherwise, returns zero (0). This will result in a list of numbers, like this: [0, 0, 3.18, 0, 1.19, 0].
  3. Sums all the numbers in the list.

This results in 4.37.

 

Advanced Example 2: Summing Repeated Metrics in an Array Nested within an Object

For this example, consider an array nested within an object in some source data:

{
"shippingData": {
"logisticsInfo": [
{"listPrice": 6.99, "status": "shipped"},
{"listPrice": 0, "status": "pending"}
]
}
}

In this case, the source object contains an object called shippingData, and that object includes an array called logisticsInfo. Each object in the logisticsInfo array contains a listPrice attribute, and we want to sum all instances of this attribute to determine the order’s total_shipping_price.

As with the Basic Example above, we will use the following functions in concert:

  • Lambda: A function to name variables used in the mapping expression.
  • Map: A Lambda helper function that returns all values of a specific variable (named in the Lambda function).
  • Get: A function to retrieve the value of an attribute within an object (the source data, in this case).
  • Sum: A function that sums the values returned by the MAP function.

However, the GET function at the end of the mapping expression will include a second (nested) GET function to pull the logisticsInfo array out of the shippingData object, like so:

GET("logisticsInfo",GET("shippingData",source,{}),[])

We can then incorporate that into the MAP function to select the listPrice values from the array like this:

MAP(
GET("listPrice",each,0),
GET(
"logisticsInfo",
GET("shippingData",source,{})
,[])
)

This returns a list of two values: [6.99, 0]. By wrapping the whole expression in a SUM function (see below), we can return the total value, or 6.99.

SUM(
MAP(
GET("listPrice",each,0),
GET(
"logisticsInfo",
GET("shippingData",source,{}),
[]
)
)
)