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 functions

Lambda functions in NebulaGraph Database Developer Edition apply operations to collection elements in persistent graphs and temporary graphs.

Lambda functions allow you to define an operation that can be applied to each element in a list by using a lambda value expression. The functions below accept lambda value expressions as arguments.

transform()

Transforms elements in a list by using a lambda value expression and returns a new list.

Null behavior: default

Function signature

transform(l: List<E>, lambda: lambda_value_expr) -> List<U>
  • l: List<E>: The list to be transformed.
  • lambda: lambda_value_expr: The lambda value expression to be applied to each element in the list, which is transformed into a new element.
  • List<U>: The transformed list returned. The data type of the elements in the new list is defined by the lambda value expression, which means that the data type of the elements in the new list can be different from the data type of the elements in the original list.

Examples

Transform a list of integers to a list of integers by multiplying each element by 2 and adding 1

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

Return:

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

Transform a list of integers to a list of integers by using a case expression

RETURN transform(LIST[1, 2, 3], x -> CASE WHEN x % 2 = 0 THEN 2 * x ELSE 0 END) AS a

Return:

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

The preceding example uses a case expression to multiply even elements in the list by 2 and transform odd elements to 0.

Transform a list of integers to a list of booleans

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

Return:

+--------------------+
| a                  |
+--------------------+
| [false,false,true] |
+--------------------+

Transform a list of nodes to a list of node IDs

'''
USE movie 
MATCH p=(m:Movie)<-[e:Watch]-(n@`User`) 
RETURN transform(nodes(p), x -> x.id) AS a 
LIMIT 5
'''

The preceding example is based on the example dataset. The example first matches a path p and then transforms the nodes in the path p to their IDs, and returns the first five transformed lists.

Return:

+-----------+
| a         |
+-----------+
| [22,143]  |
| [473,143] |
| [298,111] |
| [22,111]  |
| [496,111] |
+-----------+

filter()

Filters elements in a list by using a lambda value expression and returns a new list.

Null behavior: default

Function signature

filter(l: List<E>, lambda: lambda_value_expr) -> List<E>
  • l: List<E>: The list to be filtered.
  • lambda: lambda_value_expr: The lambda value expression to be applied to each element in the list. If the expression returns true, the element is retained in the new list; otherwise, the element is removed.
  • List<E>: The filtered list returned. The data type of the elements in the new list is the same as the data type of the elements in the original list.

Examples

Filter a list of integers to retain elements greater than 2

RETURN filter(LIST[1, 2, 3], x -> x > 2) AS a

Return:

+-----+
| a   |
+-----+
| [3] |
+-----+

Filter a list of integers to retain even numbers

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

Return:

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

reduce()

Applies a lambda value expression to each element in a list, performs cumulative calculations, and returns a single value.

Null behavior: default

Function signature

reduce( l: list<E>, state: S, input_function: (S, E) -> S ) -> S

or:

reduce( l: list<E>, state: S, input_function: (S, E) -> S, output_function: (S) -> R ) -> R
  • l: list<E>: The list to be processed. E represents elements of any data type in the list.
  • state: S: The initial state. This value is updated each time input_function is applied to an element in the list. The data type of the initial state can be any data type and can differ from the data type of the list elements.
  • input_function: (S, E) -> S: The lambda value expression applied to each element in the list.
  • output_function: (S) -> R: (Optional) A lambda expression that transforms the cumulative result into the final output.
  • S: The cumulative result after input_function is applied to all elements in the list. The data type of the cumulative result is the same as that of the initial state.
  • R: The final output after the cumulative result is transformed using output_function. The data type of the final output can differ from the data type of the cumulative result.

Examples

Calculate the sum of an integer list

RETURN reduce(range(1,3), 100, (state, item) -> state + item) AS a

Return:

+-----+
| a   |
+-----+
| 106 |
+-----+

In the preceding statement, range(1,3) generates an integer list [1, 2, 3]. The function reduce() accumulates each element in the list to the initial value 100 using the lambda value expression (state, item) -> state + item. The result is 100 + 1 + 2 + 3 = 106.

Take an empty list as input

RETURN reduce([], 100, (state, item) -> item + 3) AS a

Return:

+-----+
| a   |
+-----+
| 100 |
+-----+

In the preceding statement, the initial value for the function reduce() is 100. Since the input list is empty, the lambda value expression (state, item) -> item + 3 is not applied, and the cumulative result remains the initial value 100.

Use a lambda value expression to transform the output result

'''
RETURN reduce([1, 2, 3, 4],
       {sum: 0.0, count: 0},
       (state, item) -> {sum: state.sum + item, count: state.count + 1},
       state -> CASE WHEN state.count = 0 THEN NULL ELSE state END) AS a
'''       

Return:

+--------------------+
| a                  |
+--------------------+
| {count:4,sum:10.0} |
+--------------------+

In the preceding statement, the initial value for the function reduce() is {sum: 0.0, count: 0}. The function uses the lambda value expression (state, item) -> {sum: state.sum + item, count: state.count + 1} to accumulate each element in the list to sum and increment count by 1. Then, the lambda value expression state -> CASE WHEN state.count = 0 THEN NULL ELSE state END returns the cumulative result as the output result after evaluation.