Lambda

params => expression
Lambda( params, expression )
Lambda( params, expression )
Lambda { params => expression }
lambda_( params, expression )
| 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.

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.

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

fmt.Println(result)
client.query(
  Map(Arr("Hen "), Lambda { name => Concat(Arr(name, "Wen")) }))
client.query(
  q.map_expr(q.lambda_("name", q.concat([q.var("name"), "Wen")), ["Hen "]))
$client.query do
  map ['Hen '] do |name|
    concat([name, 'Wen'])
  end
end
client.query(
    Map(
        collection: Arr("Hen "),
        to: { name in Concat(name, "Wen") }
    )
)
client.query(
  q.Map(
    ["Hen "],
    q.Lambda("name", q.Concat([q.Var("name"), "Wen"]))))
.then((ret) => console.log(ret))
HTTP/1.1 200 OK
{ "resource": [ "Hen Wen" ] }
[ "Hen Wen" ]
["Hen Wen"]
[Hen Wen]
[ "Hen Wen" ]
[ "Hen Wen" ]
[ "Hen Wen" ]
[ "Hen 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.

curl https://db.fauna.com/ \
    -u fnAChGwBcAACAO70ziE0cfROosNJHdgBmJU1PgpL: \
    -d '{
          "map": {
            "lambda": [ "f", "l" ],
            "expr": {
              "concat": [ { "var": "f" }, { "var": "l" } ],
              "separator": " "
            }
          },
          "collection": [ [ "Hen", "Wen" ] ]
        }'
client.Query(
  Map(Arr(Arr("Hen", "Wen")), f, l) => Concat(Arr(f, l), " ")));
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());
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)
client.query(
  Map(
    Arr(Arr("Hen", "Wen")),
    Lambda { (f, l) => Concat(Arr(f, l), " ") }))
client.query(
  q.map_expr(
    lambda f, l: q.concat([f, l], " "),
    [["Hen", "Wen"]]
  ))
$client.query do
  map [['Hen', 'Wen']] do |f, l|
    concat([f, l], ' ')
  end
end
client.query(
    Map(
        collection: Arr(Arr("Hen", "Wen")),
        to: { (f, l) in Concat(f, l, separator: " ") }
    )
)
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))
HTTP/1.1 200 OK
{ "resource": [ "Hen Wen" ] }
[ "Hen Wen" ]
["Hen Wen"]
[Hen Wen]
[ "Hen Wen" ]
[ "Hen Wen" ]
[ "Hen Wen" ]
[ "Hen Wen" ]
[ 'Hen Wen' ]

The query below passes more arguments to the Lambda than the Lambda expects. In this case, the (i.e., 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.

curl https://db.fauna.com/ \
    -u fnAChGwBcAACAO70ziE0cfROosNJHdgBmJU1PgpL: \
    -d '{
          "map": { "lambda": [ "f", "_" ], "expr": { "var": "f" } },
          "collection": [ [ "Hen", "Wen" ] ]
        }'
client.Query(
  Map(Arr(Arr("Hen", "Wen")), Lambda(Arr("f", "_"), Var("f"))));
System.out.println(
      client.query(
        Map(
          Arr(Arr(Value("Hen"), Value("Wen"))),
          Lambda(
             Arr(Value("f"),Value("_")),
             Concat( Arr(Var("f")), Value(" "))
          )
        )
      ).get());
result, _ :=  client.Query(
    f.Map(
        f.Arr{f.Arr{"Hen", "Wen"}},
        f.Lambda(f.Arr{"f", "_"}, f.Var("f")),
    ),
)

fmt.Println(result)
client.query(
  Map(Arr(Arr("Hen", "Wen")), Lambda { (f, _) => f }))
client.query(
  q.map_expr(
    q.lambda_expr(["f", "_"], q.var("f")),
    [["Hen", "Wen"]]
  ))
$client.query do
  map [['Hen', 'Wen']], lambda_expr(['f', '_'], var('f'))
end
client.query(
    Map(
        collection: Arr(Arr("Hen", "Wen")),
        to: Lambda(vars: "f", "_" in: Var("f"))
    )
)
client.query(q.Map([["Hen", "Wen"]], q.Lambda(["f", "_"], q.Var("f"), q.Value(" ")))
  .then((ret) => console.log(ret))
HTTP/1.1 200 OK
{ "resource": [ "Hen" ] }
[ "Hen" ]
["Hen"]
[Hen]
[ "Hen" ]
[ "Hen" ]
[ "Hen" ]
[ "Hen" ]
[ 'Hen' ]