Lambda

params => expression
Lambda( params, expression )
Lambda( params, expression )
Lambda( params, expression )
lambda_( params, expression )
Lambda { params => expression }

Description

The Lambda function is an anonymous function that performs lazy execution of custom code. It allows you to organize and execute almost any of the Fauna Query Language statements. A Lambda can take zero or more arguments. Lambdas that define multiple parameters use a "params" array to define the arguments. In this case, the items inside the "params" array are the arguments, not the array itself. The params array must have the same number of elements as the Lambda function expects, or an _ (i.e., underscore) argument to drop the extra arguments in the array. Otherwise, it will return an error. The Lambda arguments may be accessed inside the Lambda code using the Var statement.

Two functions are considered equal if their syntax is identical. For example:

Equals(Query(Lambda('a', 5)), Query(Lambda('a', 5)))
true

Parameters

Argument Type Definition and Requirements

params

Value or Array

A single argument, or an array of zero or more arguments.

expression

Expressions

An expression to be evaluated.

Returns

The evaluation of the expression.

Examples

The query below uses a Map function to provide a single argument to the Lambda. The Lambda takes the parameter called name, resolves it to the value "Hen ", and then provides it to the first parameter to concatenate with the string "Wen". The result of "Hen Wen" is then returned.

client.Query(Map(Arr("Hen "), name => Concat(Arr(name, "Wen"))));
[ "Hen Wen" ]
curl https://db.fauna.com/ \
    -u fnAChGwBcAACAO70ziE0cfROosNJHdgBmJU1PgpL: \
    -d '{
          "map": {
            "lambda": "name",
            "expr": { "concat": [ { "var": "name" }, "Wen" ] }
          },
          "collection": [ "Hen " ]
        }'
HTTP/1.1 200 OK
{ "resource": [ "Hen Wen" ] }
result, _ := client.Query(
    f.Map(
        f.Arr{"Hen "},
        f.Lambda("name", f.Concat(f.Arr{f.Var("name"), "Wen"})),
    ),
)

fmt.Println(result)
[Hen Wen]
System.out.println(
      client.query(
        Map(
          Arr( Value("Hen ")),
          Lambda(
            Value("name"),
            Concat(Arr(Var("name"), Value("Wen")))
          )
        )
      ).get());
["Hen Wen"]
client.query(
  q.Map(
    ['Hen '],
    q.Lambda('name', q.Concat([q.Var('name'), 'Wen'])),
  )
)
.then((ret) => console.log(ret))
[ 'Hen Wen' ]
client.query(
  q.map_(q.lambda_("name", q.concat([q.var("name"), "Wen")), ["Hen "]))
[ "Hen Wen" ]
client.query(
  Map(Arr("Hen "), Lambda { name => Concat(Arr(name, "Wen")) }))
[ "Hen Wen" ]

The query below passes multiple arguments to a Lambda. The number of values in the array passed into the Lambda and the number of arguments in the Lambda must match exactly. In this example, the Map passes an array with two elements and the Lambda takes an array with two elements. The Lambda resolves the two arguments to their values and then calls the Concat function with the values.

client.Query(
  Map(Arr(Arr("Hen", "Wen")), f, l) => Concat(Arr(f, l), " ")));
[ "Hen Wen" ]
curl https://db.fauna.com/ \
    -u fnAChGwBcAACAO70ziE0cfROosNJHdgBmJU1PgpL: \
    -d '{
          "map": {
            "lambda": [ "f", "l" ],
            "expr": {
              "concat": [ { "var": "f" }, { "var": "l" } ],
              "separator": " "
            }
          },
          "collection": [ [ "Hen", "Wen" ] ]
        }'
HTTP/1.1 200 OK
{ "resource": [ "Hen Wen" ] }
result, _ := client.Query(
    f.Map(
        f.Arr{f.Arr{"Hen", "Wen"}},
        f.Lambda(
            f.Arr{"f", "l"},
            f.Concat(
                f.Arr{f.Var("f"), f.Var("l")},
                f.Separator(" "),
            ),
        ),
    ),
)

fmt.Println(result)
[Hen Wen]
System.out.println(
      client.query(
        Map(
          Arr(
            Arr(Value("Hen"), Value("Wen"))
          ),
          Lambda(
            Arr(Value("f"),Value("l")),
            Concat(Arr(Var("f"), Var("l")), Value(" "))
          )
        )
      ).get());
["Hen Wen"]
client.query(
  q.Map(
    [['Hen', 'Wen']],
    q.Lambda(
      ['f', 'l'],
      q.Concat([q.Var('f'), q.Var('l')], q.Value(' '))
    ),
  )
)
.then((ret) => console.log(ret))
[ 'Hen Wen' ]
client.query(
  q.map_(
    lambda f, l: q.concat([f, l], " "),
    [["Hen", "Wen"]]
  ))
[ "Hen Wen" ]
client.query(
  Map(
    Arr(Arr("Hen", "Wen")),
    Lambda { (f, l) => Concat(Arr(f, l), " ") }))
[ "Hen Wen" ]

The query below passes more arguments to the Lambda than the Lambda expects. In this case, the _ (underscore) has been provided to the params array so that the Lambda function knows to discard the extra arguments. If the _ had not been provided, this function would have returned an error.

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

fmt.Println(result)
[Hen]
System.out.println(
      client.query(
        Map(
          Arr(Arr(Value("Hen"), Value("Wen"))),
          Lambda(
             Arr(Value("f"),Value("_")),
             Concat( Arr(Var("f")), Value(" "))
          )
        )
      ).get());
["Hen"]
client.query(
  q.Map(
    [['Hen', 'Wen']],
    q.Lambda(['f', '_'], q.Var('f'), q.Value(' ')),
  )
)
.then((ret) => console.log(ret))
[ 'Hen' ]
client.query(
  q.map_(
    q.lambda_(["f", "_"], q.var("f")),
    [["Hen", "Wen"]]
  ))
[ "Hen" ]
client.query(
  Map(Arr(Arr("Hen", "Wen")), Lambda { (f, _) => f }))
[ "Hen" ]

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!