Map

Map( array, lambda )
Map( array, lambda )
Map( array, lambda )
Map( array, lambda )
map_( lambda, array )
Map( array, lambda )

Description

The Map function iterates on the provided array, calling the provided lambda function repeatedly with each item in array, and returns the results of all invocations in a new array of the same type (an Array or Page). As Map processes array, each invocation of the lambda function can see the effects of write operations from previous invocations.

Parameters

Argument Type Definition and Requirements

array

Array or Page

The target array over which the lambda function iterates/operates.

lambda

The anonymous function to be executed. It must accept one argument, which is the current item from array that is being processed.

Returns

A new array with the results of calling the lambda function on each item in array.

Examples

The following query has a Lambda which takes one variable x, adds 1 to its value, and then returns the incremented value. This Lambda is executed once for each value in the array. These values are 1, 2, 3.

client.Query(Map(Arr(1, 2, 3), x => Add(x, 1)));
[ 2, 3, 4 ]
curl https://db.fauna.com/ \
    -u fnAChGwBcAACAO70ziE0cfROosNJHdgBmJU1PgpL: \
    -d '{
          "map": { "lambda": "x", "expr": { "add": [ { "var": "x" }, 1 ] } },
          "collection": [ 1, 2, 3 ]
        }'
HTTP/1.1 200 OK
{ "resource": [ 2, 3, 4 ] }
result, _ := client.Query(
    f.Map(f.Arr{1, 2, 3}, f.Lambda("x", f.Add(f.Var("x"), 1))),
)

fmt.Println(result)
[2 3 4]
System.out.println(
  client.query(
    Map(
       Arr(Value(1), Value(2), Value(3)),
       Lambda(Value("x"), Add(Var("x"), Value(1)))
    )
  ).get());
[2, 3, 4]
client.query(
  q.Map([1, 2, 3], q.Lambda('x', q.Add(q.Var('x'), 1)))
)
.then((ret) => console.log(ret))
[ 2, 3, 4 ]
client.query(q.map_(lambda x: q.add(x, 1), [1, 2, 3]))
[ 2, 3, 4 ]
client.query(Map(Arr(1, 2, 3), Lambda { x => Add(x, 1) }))
[ 2, 3, 4 ]

Is this article helpful? 

Tell Fauna how the article can be improved:
Visit Fauna's forums or email docs@fauna.com

Thank you for your feedback!