Just watched Gary Bernhardt’s “wat”. JavaScript:
It is so nice to work with a sane programming language
rather than this lunacy!
Julia isn’t exactly immune to this kind of interesting behavior. What does
function g(a)
if a
f() = 2
else
f() = 3
end
return f
end
do? (disallow methods of local functions in different blocks · Issue #15602 · JuliaLang/julia · GitHub)
Yes, this one is weird. It does have a “right” way solution though:
julia> function g(a)
if a
f = () -> 2
else
f = () -> 3
end
return f
end
g (generic function with 1 method)
julia> g(true)()
2
julia> g(false)()
3
julia>
The wrong way just needs to be caught by the compiler, I think.
By the way, if you know of some weirdness in Julia, do post it here please.
f = 2
f1 = 2
2f + 2f1
edit: heh, nice, discourse highlighting gives it away
julia> a = -1
-1
julia> 1^a === 1^(-1)
false
julia> a = 1; b = 2; a + b = 3;
julia> 1 + 4
3
julia> a = 1 + 2
- 3
-3
julia> a
3
Nice! Should
julia> x + y = 2
+ (generic function with 1 method)
just be forbidden?
julia> a = 1; b = 2; a + b = 3
ERROR: error in method definition: function Base.+ must be explicitly imported to be extended
Stacktrace:
[1] top-level scope at none:0
[2] top-level scope at REPL[10]:1
A bit of protection…
If you have used Base.+
, in the module, yes.
Haha! What a fantastic thread! For readers not so familiar with Julia, most of these are mere curiosities, I’ve never had any of them creep up on me (save perhaps thinking I was adding methods to Base functions without remembering to import them first). Julia is highly consistent and sane in my experience…
Numbers being iterable and trailing 1 indexes can be somewhat surprising:
julia> size(1.0)
()
julia> 1.0[1, 1, 1]
1.0
julia> collect(1.0)
0-dimensional Array{Float64,0}:
1.0
I encountered both wats above in real-world code. The worst was when had a complicated interpolation scheme where I sampled a function f(x,y)
at points 0, 1 and 2 in both directions, and wanted to compute a second derivative with (f02 - 2f01 + f00)
. That was a fun hour debugging why my results were wrong… (and not even completely wrong, since my function f was about 10…) In general though I agree with you, julia is pretty consistent.
julia> where where where where where where where
Any
This seems to be happening with any odd number of where
greater than 1?
It is basically
T where T where S
with the symbol where
instead of T
and S
.
I always thought writing 1x
and 2f
instead of 1*x
and 2*f
was a bad idea.
As my father used to say “just because you could doesn’t mean you should”.
I much prefer 2π
over 2*π
, which is better still than 2*pi
or the ugliest =2*PI()
. Also I like being able to write √(2log(2))
.
But every once in a while I get carried away and try to write 2πf
, which doesn’t work out so well.
In this case the best is to use tau .
How many times have I or my students been caught out by for i in n
instead of for i in 1:n
. I really wish the former was just made a syntax error.
Conversely, I often find this syntax convenient.