Using the wrong function defintion, many old methods



I was trying different ways to speed up my code and found something strange.
Please, compare these two functions:

function f1(n)
    s = 0
 for i in 2:n
        s += sum(( ((j^2-1)/(j-1) ) for j in 2:i)
    return s

f1(5) gives 105

function f2(n)
    s = 0
 for i in 2:n
        s += sum( j+1 ) for j in 2:i)
    return s

f2(5) gives 40.

(j^2-1)/(j-1) should be the same than (j+1)

What am I doing wrong?


please post runnable code - s += sum( j+1 ) for j in 2:i) has an uneven number of brackets


After removing the offending bracket in f2, I get

julia> f1(5)

julia> f2(5)


Oh, my fault I’ve found the problem.

I defined f1 several times with slight changes and Julia created different methods for them.
I guess when I executed f1(5) it was using an old method inadvertedly. I’m new to Julia.
I’ve restarted Julia and now it’s OK.
I’m going to change the title of the thread.

How can I delete an old function definition or be sure Julia is using the proper one?


Which version of Julia are you using? If you are redefining a method with the same type signature, it should automatically overwrite the old one.


You can’t do that if it’s in Main’s global scope - it’s one of the cases where you need to restart Julia.
You can do it by defining your function in a module and not exporting them though. Reloading module MyModule will then only have the methods defined for MyModule.f1 that you specify.