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(var1, expr1, ... , varN, exprN, expr)

WITH takes an indeterminate number of variable binding arguments

Arguments

Definitions

var1

First variable name

expr1

First expression; the result of this expression is assigned to var1 which then can be used within the final expr

varN

Nth variable name

exprN

Nth expression, assigned to varN

expr

An expression 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]