Common programming languages use standard constructs like if ... else or condensed statements such as [condition] ? [expression if true] ? [expression if false], Aiming to allow for more dense and efficient coding, B4P provides a set of effective comparison and selection functions. The functions come in two fragrances:
In these functions all parameters are fully calculated regardless of the condition and/or if the values are finally selected or discarded. This approach is most efficient if the values are constants or simple expression (e.g. a constant, referring to variable, a table entry, a simple calculation, etc.) or if manipulations must be made in any case, e.g. doing a function call or incrementing/decrementing variables.
a[] = select if( true, echo(yes), echo(no) ); // Note. Both 'yes' and 'no' are printed here
echo("Result is ", a[] );
yes
no
Result is yes
In these functions, the expressions for the values to select are only calculated if the results are needed. Otherwise, these expressions will be skipped. This approach saves a lot of computation effort if more sophisticated expressions are contained. In addition, if a variable in an expression not selected is not defined, this does not matter because that expression is skipped.
a[] = pick if( true, echo(yes), echo(no) ); // Note: Only 'yes' if printed
echo("Result is ", a[] );
yes
Result is yes