A common thing in programming, a good example being JavaScript, is using ternary if
statements to assign values to a variable.
For example:
const speed = time === 0 ? undefined : (distance / time);
Often times you can shorten code such as
if (condition) {
f('test', 5);
} else {
f('test', 6);
}
to just
f('test', condition ? 5 : 6);
by moving the conditional inside the function.
This seems pretty similar to something I remember from learning Calculus in college where you could move a $\lim$ inside if $f$ was continuous at some point.
From https://math.stackexchange.com/a/1519038/761845:
if function $f$ is continuous at $g$ and $\lim\limits_{x\to x_0} g(x)=g$ then:
$$\lim_{x\to x_0}f(g(x))=f\left(\lim_{x\to x_0} g(x)\right)$$
Is this just coincidence that these are similar looking or are the same mathematics at work?
If so, what are those mathematics?
f(g(h(x))) = g(f(h(x)))
e.g. But there's no notion of limit in programming. In fact most of the calculus does not fit good into programming. It's discrete math which is more closely related and mostly used in programming. – peter.petrov Apr 09 '21 at 15:43if
. There is no actual commutating taking place; this is closely similar tovar temp = g(x); return f(temp)
versusreturn f(g(x))
. Thus the ternary contruct works for arbitrary $f$, the limit swapping works only under continuity assumptions. – Hagen von Eitzen Apr 09 '21 at 15:44