As I am jumping on a project where I will code mainly in Julia, I thought it was worth spending some time to remind myself about core Julia’s features. I have already spent some reading reading Julia’s documentation which is great and definitively worth reading, but I was looking for something shorter and found ‘Julia language: a concise tutorial’ (Github repository available here), which by the way is listed in the Julia tutorials page. IMHO, this is a good resource to get started with Julia (only a few hours to go through) as well as a good refresher course. Below I took some notes that should mainly act as a personal reminder, but might be useful for other.
Using Greek letters
In Julia variables can be Greek letters. I used to switch to the Greek keyboard to type these, but there is also the option to use the LaTex syntax + tabulation, e.g. type
Tab\ to get
α in the prompt. This actually also applies to various LaTeX symbols, e.g.
\in for $\in$ or
\infty for $\infty$.
There is an alternative and simpler syntax for double loops:
Do blocks are an alternative syntax for anonymous functions:
Here is a list of useful functions to manipulate vectors:
Arrays, tuples and dictionaries
For arrays (matrices, vectors), use
 and element of the same type (type migh be
Any), for tuples, use
Example of arrays
A named tuple:
Overall, NamedTuple are generally more efficient and should be thought more as anonymous
struct(see the “Custom structure” section) than Dictionaries.
Convert a tuple in a vector:
Convert an array in tuple:
When applying a function to a vector, one may encounter the following error:
Instead of adding a morph to the function, one may simply use the
Or, even simpler, the broadcast mechanism (
As mentioned in the tutorial:
While in the past broadcast was available on a limited number of core functions only, the
f.()syntax is now automatically available for any function, including the ones you define.
Here is a list of functions to manipulate types
supertype(MyType)Returns the parent types of a type
subtypes(MyType)Lists all children of a type
fieldnames(MyType)Queries all the fields of a structure
objis of type
typeof(obj)Returns the type of
a::Bmeans “a must be of type B”
A<:Bmeans “A must be a subtype of B”.
Although obviously less flexible, immutable structures are much faster.
Polymorphism and Parametric Methods
First, in the declaration of a Julia function, position arguments come first then followed by keyword arguments, the two are separated by a semicolon. Default values may be applied to both kind of arguments, position arguments cannot be preceded by their name, keyword arguments must be called by their name.
In the example above different methods per function are used to account for the default value, so for instance if
myfun(1) is used the method where
b is set to 1 and
c is set to 2 will be used, but with
myfun(1, 3), the second method where only
c as a default value will be used:
And Julia is capable to keep stacking methods, for instance if parameters becomes string, a new method can be added:
Of course, this requires to be cautious as one should then think about whether the correct method will be used in the right context. Furthermore, Parametric Methods introduce type parameters that are an efficient way to design method based on argument type, so if I re-write the two methods above using this, I would write something like this:
Note that this allows to constrains the parameter types, in the example above all the three argument must be of the same type:
Last, below is an example with two different types:
I should spend more time reading about metaprogramming in Julia. As for now, I will keep in mind that
:pos declares “pos” as a symbol (equivalent to
Symbol(pos)) and is the representation of a variable name (a value value may or may not be assigned to it) and that symbol are super useful, e.g for subsetting purpose, (see above).