Skip to content
Developer Edition runs on one host and does not require a product license file. Need multi-host deployment or performance testing at scale? Explore Enterprise Edition

Lambda value expression

Lambda value expressions, also known as lambda expressions, are used to define an operation to be applied to each element in a list. Lambda value expressions take the form x -> expr, where x is the input parameter and expr is the expression body.

Lambda value expressions can be used only in functions that accept them as arguments, that is, lambda functions. For more information, see Lambda functions.

Syntax

lambda_value_expression
variable -> value_expression

lambda_value_expression ::=
    variable '->' value_expression

Parameters

variable

The name of the input parameter in the lambda value expression.

value_expression

The expression body of the lambda value expression. The expression body can be any value expression except for aggregate and subquery value expressions. For more information about value expressions, see Value expressions.

Examples

The following example is based on the example dataset.

Apply the lambda function transform() to a list

RETURN transform(LIST[1, 2, 3], x -> 2 * x + 1) AS a

Return:

+---------+
| a       |
+---------+
| [3,5,7] |
+---------+

In the preceding example, x -> 2 * x + 1 is a lambda value expression. transform(LIST[1, 2, 3], x -> 2 * x + 1) is a function value expression and transform() is a lambda function. The lambda value expression x -> 2 * x + 1 is applied to each element in the list [1, 2, 3] to transform it into a new element. The transformed list is returned as a.

Apply the lambda function filter() to a list

RETURN filter(LIST[1, 2, 3, 4, 5], x -> x % 2 = 0) AS a

Return:

+-------+
| a     |
+-------+
| [2,4] |
+-------+

In the preceding example, x -> x % 2 = 0 is a lambda value expression. filter(LIST[1, 2, 3, 4, 5], x -> x % 2 = 0) is a function value expression and filter() is a lambda function. The lambda value expression x -> x % 2 = 0 is applied to each element in the list [1, 2, 3, 4, 5] to filter out the elements that are not divisible by 2. The filtered list is returned as a.

Apply the lambda function reduce() to a list

RETURN reduce(LIST[1, 2, 3, 4, 5], 0, (acc, x) -> acc + x) AS a

Return:

+----+
| a  |
+----+
| 15 |
+----+

In the preceding example, (acc, x) -> acc + x is a lambda value expression. reduce(LIST[1, 2, 3, 4, 5], 0, (acc, x) -> acc + x) is a function value expression and reduce() is a lambda function. The lambda value expression (acc, x) -> acc + x is applied to each element in the list [1, 2, 3, 4, 5] to reduce the list into a single value. The reduced value is returned as a.

Reference an outer variable in a lambda value expression

'''
VALUE `offset` = 10
RETURN transform(LIST[1, 2, 3], x -> x + `offset`) AS a
'''

Return:

+------------+
| a          |
+------------+
| [11,12,13] |
+------------+