Fortran adds conditional expressions
j3-fortran.org
j3-fortran.org
Before there were neural networks and image-hacking, there was the gritty math-hacking to optimize big gobs of interrelated stuff that had to happen in the right order. It was almost always done in Fortran, and I suspect a lot of it still is. (And if it was not done in Fortran, it was done in APL. Ask an Actuarial scientist, if you doubt this..) Please don't laugh at Fortran. It can not only help you fly your spaceship to another planet (and get you home again!), it can also make you (and your client) rich, since you can build solid, bulletproof code from those old libraries that just works, and hence create flaw-free solutions to really critically important applications. This proposed extension looks useful.
(https://github.com/wch/r-source says that's 23% of the source. It includes vendored libraries, like a 5MB .f of LAPACK code. I bet a lot of the C code in R was f2c'd at some point in the past)
const x = y || 3;
?
$y>5 || die "y should be greater than 5";Incidentally, gfortran treats the reduction like C, which seems a deficiency as the standard allows it not to.
Perhaps the difference with the conditional discussed here is that in a function all the arguments are usually calculated before evaluation, so the conditional may avoid an error if one of the arguments can be invalid depending on the condition. It's a bit unclear from the proposed language addition, however.
y = ( i>=1 .And. i<=Size(a) ? a(i) : -Huge(y) )
The proposal has been approved, but the final standard will be voted on, and it will take time for it to appear in compilers. y = merge(a(i), -Huge(y), i>=1 .And. i<=Size(a))
but this is risky, because merge does not short-circuit, and the program could crash if i < 1. The ternary syntax y = (boolean ? expression_1 : expression_2)
only evaluates one of the expressions.I don't recall him saying that while I used to read that group, but I would have chuckled.
I understand there are conditional assignment instructions, though optimizer could likely choose these already. Also branching prediction may take care of such assignments as well.
if (i>=1 .And. i<=Size(a)) then
y = a(i)
else
y = -Huge(y)
end if const y = (i <== 1 && i < a.length()) ? a[i] : -Huge(i);
than: let y; // What do I initialize this too.
if (i <== 1 && i < a.length()) {
y = a[i]; // a side effect
} else {
y = -Huge(i); // another side effect
}
And you end up with a mutable variable that might change later on (true only for language with mutable/non mutable variable of course).Besides, it blends well with functional programming where side effect are better avoided.
associate (x=>(complicated variable))
if (test) then
x = …
else
x = …
end if
end associateGetting offtopic here from FORTRAN, but there have been several attempts at numeric libraries in javascript (math.js, numjs), they are either lightweight, or unmaintained and in a sorry state, or require async execution that expect remote hardware (Googles tensor libs) which makes them a bit difficult to use in a general setting.
Hopefully WebASM will result in a native numpy-like library for JS because numpy is a very well-thought-out library and interface, it's really addictive.