Map and Filter Functions
• Dark
Light
• PDF

# Map and Filter Functions

• Dark
Light
• PDF

Article Summary

## \$.map() and \$.filter() Erlang Functions

For Set Parameters nodes, a native support of `\$.map()` and `\$.filter()` Erlang functions is available similar to the `\$.math()`, `\$.unixtime()` support. The support for `\$.map()` and `\$.filter()` is aimed at making responses from external APIs more lightweight and making tasks smaller after going through API Call nodes.

#### Functions Syntax

`map()` and `filter()` functions are used in the following construction:

`\$.map(fun(Item) -> end, arr)`
`\$.filter(fun(Item) -> end, value)`,
where
fun-supported function (see the list below);
Item-arbitrary value;

Item syntax

arr-parameter of an input task, which contains an array;
value-a single value.

A function body is written after the `->`, in which you can write allowed expressions using simple operations from ALLOWED_INF_EXPRS (sum, add, subtract, etc.), as well as some functions from ALLOWED_EXT_FUNS.

### \$.map Use Examples

Multiplying all array elements by a specified value
On a Set Parameters node input, let's take a task containing the `“a”` array:

``````"a": [1,2,3]
``````

Let's write the following \$.map function in a Set Parameters node:

``````"b": "\$.map(fun(Item) -> Item*2 end, {{a}})"
``````

With this function used, each `“а”` array element is multiplied by 2, and the results are written to `“b”` array. This results in the following `“b”` array:

``````"b": "[2,4,6]"
``````

### \$.filter Use Examples

Filtering off uneven numbers from an array and writing even numbers to another array
Suppose we have the following `"b"` task:

``````[1,2,3,4,5,6,7,8,9,10,11,12,13,14]
``````

Let's write the following function to a Set Parameters node:

``````"b1": "\$.filter(fun(Item) when Item rem 2 < 1 -> true; (_) -> false end, {{b}})"
``````

We will get the response:

``````"b1":[2,4,6,8,10,12,14]
``````

### List of supported functions

``````-define(ALLOWED_EXT_FUNS, [
{proplists,'_','_'},
{base64,'_','_'},

{binary, split, '_'},
{binary, replace, '_'},

{eutils, get_value, 2},
{eutils, from_json, '_'},
{eutils, to_json, 1},

{erlang,binary_to_float,1},
{erlang,binary_to_integer,1},

{erlang,integer_to_binary,1},
{erlang,integer_to_binary,2},

{erlang,round,1},

{erlang, is_integer, 1},
{erlang, is_binary, 1},
{erlang, is_list, 1},
{erlang, is_float, 1},
{erlang, is_boolean, 1},
{erlang, is_number, 1},

{erlang, hd, 1},
{erlang, tl, 1}

]).
``````

The list items have the following structure:
`{module,function, number of arguments}`
For example, `{eutils, to_json, 1}` means that the `to_json` function from the `eutils` module is supported with the number of arguments equal to 1.
`'_'` means an unlimited number of either supported arguments or functions, or both.

``````-define(ALLOWED_INF_EXPRS, [
'+',
'-',
'*',
'/',
'bnot',
'div',
'rem',
'band',
'bor',
'bxor',
'bsl',
'bsr',
'not',
'and',
'or',
'xor',
'andalso',
'orelse',
'==',
'/=',
'=<',
'<',
'>=',
'>',
'=:=',
'=/=',
'++',
'--'
]).
``````