Skip to contents

Conditional execution of two branches. Unlike nv_ifelse(), which selects elements, this executes only one of the two branches depending on a scalar predicate.

Usage

nv_if(pred, true, false)

Arguments

pred

(arrayish)
Predicate. Must be a scalar of the boolean data type, or an R logical.

true

(function())
Zero-argument function for the true branch.

false

(function())
Zero-argument function for the false branch. Must return the same structure, data types and shapes as the true branch; nothing is promoted.

Value

(arrayish | list)
Result of the executed branch: an array, or a tree of them in the sense of pjrt's RTree – a list, nested arbitrarily – with the structure, data types and shapes both branches share.

See also

prim_if() for the underlying primitive, nv_ifelse() for element-wise selection.

Examples

# both branches must return the same structure, data types and shapes
nv_if(nv_scalar(TRUE), \() nv_scalar(1), \() nv_scalar(2))
#> AnvlArray
#>  1
#> [ CPUf32{} ]