Does Fortran have a two-way decision function?

& (verbiage overflow)Fri 29 April 2016RSS

A “two-way decision function” usually means something that emulates the The ternary operator, an in-line if...else... construction, first introduced in C:

condition ? if_true : if_false

(Kernighan and Ritchie, The C Programming Language, second edition, secs. 2.11, A7.16.)

Some writers describe merge, from the 1995 revision of Fortran, as such a function. But it differs somewhat from a generic two-way decision. This merge performs element-by-element comparison of two arrays and produces a third array:

merge(source_if_condition_true, source_if_condition_false, condition)

This is comparable to Python

[x if condition(x) else y for x, y in zip(list_x, list_y)]

where condition() is a “mask” function returning the truth-state of some condition.

The Fortran function could model a simple two-way decision if used on single-element arrays, but its actual purpose is to recombine the arrays, element by element, hence its name.

[end]

Comments are enabled.