Lambda
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 parameters. Lambda
s that
accept multiple parameters must use a params
array to represent all of
the parameters. 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 items as the caller provides, or an error occurs.
When a caller of a Lambda
includes multiple parameters, such as
when processing index entries with multiple values
fields, your
function might not need every item in the params
array. Instead of
creating a name for an unneeded parameter, you can use an _
(underscore) instead; it signals that the function should ignore that
parameter.
The Lambda
parameters may be accessed inside the Lambda
code
using the Var
statement (except the _
parameter; it is not a
named variable, it is a placeholder for a variable that should be
ignored).
Two functions are considered equal if their syntax is identical. For example:
|
Examples
-
The following query uses the
Map
function to provide a single argument to theLambda
. TheLambda
takes the parameter calledname
, 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.Arr(StringV(Hen Wen))
[Hen Wen]
["Hen Wen"]
[ 'Hen Wen' ]
[ "Hen Wen" ]
[ 'Hen Wen' ]
-
The following query passes multiple arguments to a
Lambda
. The number of values in the array passed into theLambda
and the number of arguments in theLambda
must match exactly. In this example, theMap
passes an array with two elements and theLambda
takes an array with two elements. TheLambda
resolves the two arguments to their values and then calls theConcat
function with the values.Arr(StringV(Hen Wen))
[Hen Wen]
["Hen Wen"]
[ 'Hen Wen' ]
[ "Hen Wen" ]
[ 'Hen Wen' ]
-
The following query passes more arguments to the
Lambda
than are declared. In this case, the_
(underscore) has been provided to theparams
array so that theLambda
function knows to discard the extra arguments. If the_
had not been provided, this function would have returned an error.Arr(StringV(Hen))
[Hen]
["Hen"]
[ 'Hen' ]
[ "Hen" ]
[ 'Hen' ]
-
The following query uses the
Map
function to provide two objects to theLambda
as theuser
parameter. TheLambda
selects theemail
field from each object, which becomes part of the response:Arr(StringV(henwen@black.cauldron), StringV(taran@black.cauldron))
[henwen@black.cauldron taran@black.cauldron]
["henwen@black.cauldron", "taran@black.cauldron"]
[ 'henwen@black.cauldron', 'taran@black.cauldron' ]
['henwen@black.cauldron', 'taran@black.cauldron']
[ 'henwen@black.cauldron', 'taran@black.cauldron' ]
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!