WITH

WITH arguments occur in a closure, and as such can be defined as variables within the scope of the function arguments.

Function category: Special

Syntax

WITH([arg1...], arg2)

Arguments

Definitions

arg1

Variable binding or set of bindings, in the form of tuples, wherein the first argument is the variable name and the second argument is the variable value.

WITH

Takes an indeterminate number of variable binding arguments.

arg2

An expression that must always occur as the last argument - all previous argument pairs will constitute variable assignments. The result of this expression will be what is returned by the WITH function.

Examples

Let's say we're given a response with the following vehicle performance information.

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

Example 1: Find highest existing score as a benchmark

If we wanted relative vehicle scores, with the highest existing score serving as a benchmark, we use the following function.

# Find relative vehicle scores
# with highests existing score as benchmark
WITH(addcurve, FN(x, x + 4), MAP(addcurve, data.vehicle_ratings))
# Returns [96, 88, 75, 84, 100]

Example 2: Add second variable represents the curve

To abstract it even further, we could add a second variable that represents the curve.

# Find vehicle scores
WITH(curve, 100 - MAX(data.vehicle_ratings), addcurve, FN(x, x + curve), MAP(addcurve, data.vehicle_ratings))
# Returns [96, 88, 75, 84, 100]