# Lambda

> Source: https://docs.recombee.com/reql/functions/lambda

> For the complete documentation index, see [llms.txt](/llms.txt).

# Lambda

ReQL Lambda functions can be used for computations on sets of values.

Lambda functions take two arguments: a lambda expression and a set.

Lambda functions evaluate the lambda expression on every element of the set. The lambda expression can be any valid ReQL expression, and therefore can contain for example item properties or functions.

| Name                        | Description                                                                                | Example                                                  |
| --------------------------- | ------------------------------------------------------------------------------------------ | -------------------------------------------------------- |
| [map](#reql-func-map)       | Applies the lambda expression to every member of a set                                     | map(lambda 'x': 2\*'x', {1, 2, 3}) == {2, 4, 6}          |
| [select](#reql-func-select) | Returns only those values of input set for which the lambda expression returns true        | select(lambda 'x': 'x' \> 5, {10, 2, 13, 1}) == {10, 13} |
| [exists](#reql-func-exists) | Returns _true_ if lambda expression is satisfied for at least one element of the input set | exists(lambda 'x': 'x' \> 5, {2, 13, 4})                 |

## Map

Map applies the expression to every element and returns the result as set.

### Definition

```
map(lambda 'x': EXPRESSION, input_set)
```

| Argument          | Type              |
| ----------------- | ----------------- |
| lambda expression | lambda expression |
| input\_set        | set               |

### Examples

All the following expressions result in `true`:

```
// Multiplies every element of the input set by two
map(lambda 'x': 2\*'x', {1, 2, 3}) == {2, 4, 6}

// Converts every element from the input set to number
map(lambda 'x': number('x'), {"1", "2", "3"}) == {1, 2, 3}

// Map applied on empty set is empty set
map(lambda 'x': 'x'*'x', {}) == {}
```

## Select

Select returns only those values of input set for which the lambda expression returns true. The lambda expression have to return a boolean.

### Definition

```
select(lambda 'x': EXPRESSION, input_set)
```

| Argument          | Type                                |
| ----------------- | ----------------------------------- |
| lambda expression | lambda expression returning boolean |
| input\_set        | set                                 |

### Examples

All the following expressions result in `true`:

```
// Selects only the values that are greater than five
select(lambda 'x': 'x'>5, {10, 2, 13, 8, 3, 4, 7}) == {10, 13, 8, 7}

// ''true'' is satisfied for every element
select(lambda 'x': true , {1, "a", {}}) == {1, "a", {}}

// Selects only the strings, that are lowercase
select(lambda 'x': lower('x') == 'x', {"HELLO", "hello", "Hello", "a"}) == {"hello", "a"}

// No element represents a number with value less then 100
select(lambda 'x': number('x')<100 , {"125", "123", "251"}) == {}
```

## Exists

Exists returns _true_ if lambda expression returns _true_ for at least one element of the input set. Otherwise returns _false_.

`exists(lambda 'x': ...)` can be though as shorthand for `size(select(lambda 'x': ...)) > 0.`

### Definition

```
exists(lambda 'x': EXPRESSION, input_set)
```

| Argument          | Type                                |
| ----------------- | ----------------------------------- |
| lambda expression | lambda expression returning boolean |
| input\_set        | set                                 |

### Examples

All the following expressions result in `true`:

```
// Returns true, as 13 > 5
exists(lambda 'x': 'x'>5, {2, 13, 4})

// None of the elements is larger than 5
exists(lambda 'x': 'x'>5, {2, 1, 4}) == false
```