I am reading through the Julia manual right now and ran into my first potential disappointment.
I like being able to code conditional statements tersely. In R I might write:
if (x==y) print("Hello")
In Julia however I think I might need do
if x==y
println("Hello")
end
Or perhaps x==y ? print("Hello") : print("")
which is certainly silly.
Is there some formulation in Julia for allowing for single line conditional statements?
You can write if x == y println("Hello") end
or, as has become somewhat idiomatic, you can use the short-circuiting behavior of the &&
operator and write x == y && println("Hello")
. In a very similar fashion it is fairly common to check some condition and throw an error if it isn't met by writing something like this: size(A) == size(B) || error("size mismatch")
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With