Skip to content
Daniel Gorman edited this page May 9, 2019 · 2 revisions

The FN Function

Function Group: Special

FN denotes an anonymous function. FN, like WITH, is a closure, and as such allows variables to be defined within the argument context and subsequently referenced.

Syntax

FN([arg1...], arg2)

  • arg1 is the expected argument or set of arguments
  • FN takes an indeterminate number of arguments
  • arg1 is not required - a function can be defined that takes no external arguments
  • arg2 is the body of the function, and must always occur as the last argument. The result of this expression will be what is returned by the FN function. arg2 is required.

Uses

Let's say we're given a response with some vehicle performance information that looks like this:

{
  "data": {
    "vehicle_ratings": [92, 84, 71, 80, 96]
  }
}

If we wanted relative vehicle scores, with the highest existing score serving as a benchmark, we could use FN = to describe that and apply it passing it as the function argument to MAP:

MAP(FN(x, x + 4), data.vehicle_ratings)

This would return:

[96, 88, 75, 84, 100]

FN can also take multiple arguments. Given the following data:

{
  "data": {
    "vehicle_safety_ratings": [92, 84, 71, 80, 96],
    "vehicle_performance_ratings": [95, 81, 77, 83, 90]
  }
}

We could use FN with MAP, which can take two arguments, and generate a combined_vehicle_rating:

MAP(FN(x, y, x + y), data.vehicle_safety_ratings, data.vehicle_performance_ratings)

This will return:

[187, 165 ,148, 163, 186]
Clone this wiki locally