Behavior of Base.maximum

Hello! The following piece of code (example) prints 10 while I would have expected 5:

for i = 1:1
    it = 1:5
    f(t) = t
    b = maximum(f, it)
    f(t) = 2*t

Does someone have an explanation for that behavior? Thanks

You may need looking for ‘argmax’, which returns the index of the largest element.

Actually, no. Ignore my comments.

When you declare functions like f(x) = y this creates a global binding to f in the module as soon as it gets compiled (likewise when you declare functions with function). What you want are “anonymous functions”

f = t -> t
f = t -> 2t

It’s unfortunate that this distinction exists, but my understanding is that it would vastly complicate the compiler to fix it.


As @ExpandingMan said, a solution is to use anonymous functions. See here and the linked issue for more details:

1 Like

Ok got it. Thanks!