I have been learning the Julia programming language recently and I wanted to take a bit of time to just show some things that I found neat about the language. I may update this over time as I learn more!
I really recommend the book Practical Julia by Lee Phillips, at least one of these examples came from there.
Neat Stuff
- Environment variables are very easy to get
# export MY_ENV_VARIABLE=1234 my_var = ENV["MY_ENV_VARIABLE"] print(my_var) ## 1234
- Accessing the last item in an array has a keyword
a = [1,2,3,4] print(a[end]) # 4
- You can use numbers next to variable names to do multiplication. Use this one at your own risk, lest you introduce ambiguity!
w = 2 print(2w) # 4
- You can use rational numbers (fractions) if you wish. This even handles type promotion.
print(1//2 + 1//3) # 5//6 print(1//2 + 1//2) # 1//1 print(1//2 + 0.5) # 1.0
- You can index matrices using arrays. This was weird when I first saw it, but it is neat. You will get a vector back instead of a matrix if the results are 1-dimensional.
m = [11 12 13 14; 15 16 17 18; 19 20 21 22;] m[2, [2, 3]] # 16 17 -- Vector m[[1, 2], [3, 4]] # 13 14 # 17 18 -- Matrix
- Functions can iterate over an array with the dot notation
m = [2, 4, 6] function double(x) return 2x end double.(m) # 4, 8, 12