Basic Introduction to Julia
2015/09/21I think I should do an introduction to Julia, since I used it often in my website. Two fundamental elements are needed to know to get started with Julia: its type system and function. I hope by learning these two elements, you will appreciate how well-design is the language for scientific computing.
Let us talk about type system first. Julia has dynamic type system like other high-level languages. However, each variables has type associated with it, either by inference or explicitly specified by the programmers. for example,
The type is Julia is a place where data is stored. You can think of it as a object in object-oriented programming language like Java, without methods associated with it. But the type system does more just stores data; It provides several nice ways to organize data.
First of all, in Julia you can combine types together to form a new type, which is similar to struct in C language. In Julia, It is called a composite type. For example, we can define a type called Cat, with two data inside,
To somehow organize different, Julia have something called abstract type. Abstract type is a type you can not assign concrete values to it. It is just a name tag without data inside, however you can make a subtype of it. This provides a way to declare hierarchical relationships among data types. For example,
Another important type in Julia is parametric types. It is similar to the concept of templates in C language. It will gives us extra flexibility to express our type. For example, if we want a type call AnimalLover, we can express it by,
There are several other features in Julia types system, like type union. and immutable type. They are all interesting. But the three I have introduced should be sufficient for starters.
If we have a type that we want to use for storing data, we can construct a variable with the type,
So far we have talked about how Julia store and organize data. Next, we will talk about how it transforms or modifies it. To do that, we declare a function that operates on the data,
The function name can be overload. The function will be invoked by signature of the function call, via multiple dispatch.
In OOP, functions and data are glued together in a class. It is a good abstraction in programming in general situation, where we constantly modify state by doing side effects. But, it is not suitable abstraction for doing scientific computing. The separation of function and data is the right one, because that is what being done in mathematics - the language of science and engineering. Types of math are all number-like types. They are standalone objects. But we can doing interesting things by applying functions to it. It is like transforming a number to another number. It will be absurd to have side effects in math. for example, if we apply $+$ on $1$ and $2$, we get back $3$, then $1$ changes to let’s say $10$.
I hope this blog helps. If you want to know more details, you can read the Julia docs