This is amazing! I’m going to present to my lab in a couple weeks and this will be a great resource for everyone else.
Interesting, the reason for this is performance of captured variables in closures · Issue #15276 · JuliaLang/julia · GitHub and you can show it more succinctly with:
function not_type_stable(a)
a = a # toggle comment on this line
return (x->x+a)(1)
end
So while I guess it is true that in some cases, assignments to a variable can cause performance problems, the reason isn’t really that a
has one type and then you assign it to another type.
This is one of those habits that many appear to be blind towards. New users are so often initializing arrays as
zeros(n, 1)
rand(n, 1)
# etc.
when they almost always should use
zeros(n)
rand(n)
# etc.
They’re not even going to think about it unless it’s pointed out directly. It’s a semi-subconscious assumption about how vectors work.
Similarly, use rand()
instead of rand(1)
or rand(1)[1]
.
I hate to be pedantic*, but
julia> struct Foo
x::Vector
end
julia> Base.length(f::Foo) = length(f.x)
julia> Base.push!(f::Foo, a) = push!(f.x, a)
julia> foo=Foo([])
Foo(Any[])
julia> length(foo)
0
julia> push!(foo,1)
1-element Array{Any,1}:
1
julia> length(foo)
1
*That’s not true. I really enjoy bring pedantic . You’re off course correct.
Totally correct! Here’s another example of the same:
julia> struct Bar end
julia> Base.length(::Bar) = rand(0:1000)
julia> b = Bar()
Bar()
julia> length(b)
338
julia> length(b)
129
linspace(a,b,n) = range(a; stop=b, length=n)
logspace(a,b,n) = 10 .^ range(a; stop=b, length=n)
When I first transitioned from matlab to Julia, I set up atom snippets for some common functionality like this.
That way I could type the old matlab stuff but get the new Julia stuff without any additional function definitions. Here are some
I love to be pedantic I am wondering if we should keep using the term “immutable” for these kinds of constructs in discussions. An “(immutable) struct
” is a clearly defined concept in Julia, but Foo
above may not mesh well with what various people understand as “immutable”.
Mutability (or lack thereof) in Julia is a very narrowly defined technical term that is meaningful mostly for composite types.
Same here. An otherwise pleasant Julia experience can be brought to a longer halt if help is not accessible.
Also had this tldr-pages or this navi idea (in previous posts I called for one-liners or snippets).
We often ask posters to provide a minimum working example.
I wondered if we could expect the same from someone answering a question, or contributes to Julia?
Sounds ungrateful? Since it’s all free and based on voluntary work.
You are all doing a great job - thanks!
My only argument is that a help that is not understood is a lost effort.
Maybe you developers and helpers can go a bit further, think of the not-so-progressed, and add some TL;DR examples.