Check out v4 of the Fauna CLI

v4 of the Fauna CLI is now in beta.

The new version introduces enhancements to the developer experience, including an improved authentication workflow. To get started, check out the CLI v4 quick start.

Anonymous functions

An anonymous function is a function that doesn’t have a name, and is typically used as an argument to another function. Functions are defined and managed using the Function API.

Declaration and scope

Fauna Query Language (FQL) uses the JavaScript-style short-form arrow syntax to declare an anonymous function. Function declaration has the generalized form:

([param[, param]]) => {[statement[ statement …​] expression]}

The param can be any valid identifier including a single underscore (_), which can be repeated in the param list.

An anonymous function block can include field access constructs, operators, function arguments, and nested blocks. Variables declared in an anonymous function block are scoped to the anonymous function, but the function can access the variables in the parent block.

The last expression is the return value of the function. There is no explicit return keyword.

Simplified syntax

For a function with one parameter, the () enclosing the parameter can be omitted. If the function body consists of a single-line expression, the {} block can also be omitted. These examples are equivalent and return a value of 6:

let Double = (x) => {x + x}
Double(3)
let Double = x => x + x
Double(3)

Predicates

A predicate is an anonymous, read-only FQL function that evaluates to true, false, or null (which is interpreted as false). It acts as a test to determine if one or more conditions are met.

Some FQL methods and FSL schema properties accept predicates. For example, set.where() accepts a predicate as its argument:

Customer.all().where(c => c.address.postalCode == "20220" && c.name == "Alice Appleseed")

Shorthand syntax

Some contexts support a shorthand syntax that lets you omit the parameter name and arrow (=>). For example, the following query is equivalent to the previous one:

Customer.all().where(.address.postalCode == "20220" && .name == "Alice Appleseed")

The syntax supports dot notation and bracket notation. The following query is equivalent to the previous one:

Customer.all().where(.["address"]["postalCode"] == "20220" && .["name"] == "Alice Appleseed")

Variadic arguments

Use the ... syntax to create a variadic function that accepts an indefinite number of arguments, including zero.

let getLength = (...args) => args.length
getLength(1, 2, 3)
3

A function can only accept one variadic argument. It must be the last argument.

Variadic arguments are collected into an Array. You can define a type signature to limit the types of values accepted and held in the Array.

For example, the following function accepts a single String argument followed by a variadic argument of zero or more Numbers:

let formatCurrency: (String, ...Number) => String = (symbol, ...amounts) => {
  symbol + amounts.reduce((prev, cur) => prev + cur).toString()
}

formatCurrency("$", 2, 3)
"$5"

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!