Julia vs Python
Julia is a challenger to Python in science, each with an extensive library. The newcomer can it replace the old? (Python dates from 1991).
Example of Julia code
The development of Julia began in 2009 and the language became popular when the compiler was open sourced in 2012. It is currently available under the MIT license. The objective is to create a language with the advantages of Python (simplicity and dynamism), R (statistical processing), C (execution speed), Perl (string processing), Matlab (linear algebra), and others. It also wants to be distributed, parallel, generic.
In the tradition of Pascal, the language name comes from the French mathematician Gaston Julia, who discovered fractals.
Its simplicity and its scientific capabilities make it a possible alternative to Python. But is it superior? We will not compare the program execution speed because Julia is new and still under development, while Python is optimized for decades and therefore close to its limits. Which one is faster to execute a particular algorithm is significant on the cloud, but on the desktop, what matters is how they behave on large data sets, matrix calculations for example. Some Julia programs are faster than the Python version, but this is not always the case. It really depends on if we call functions compiled to binary, which is the case with most Python libraries, or if they are written in Julia which is necessarily less efficient.
Instead, we compare features and syntaxes. Both languages are meant easy to understand and are aimed primarily at an audience that has no interest in the hardware, and thus in derivatives of C such as the Go language, that are done to simplify the task of the compiler and not improve programmer productivity.
- They can be used from the command line and in interactive mode.
- Dynamic variables.
- Nested functions.
- The quest for the most concise code.
- Python uses indentation to denote the hierarchy of program blocks and thus the end. Julia is more classic and all construct terminates by end as does Matlab (the origin of this usage dates back to Algol).
When ends accumulate in an algorithm, that makes it difficult to know which terminates what block. But modern editors display lines that show the hierarchy of blocks (like the picture on the right), so it is not a big problem. They must support the language, that will come with time.
- Python is interpreted, even if there are compiled versions in C or Java bytecode.
Julia uses the LLVM JIT compiler and behaves like an interpreter. So there is a similarity in use, but a different backend.
- Julia arrays are 1-based indexing.
- Homoiconicity. As Tcl or Prolog, a Julia program is implemented as a data representation. This allows the program to handle himself, to transform, to expand itself. Or to generate another program and run it, which would be very useful in robotics.
- Polymorphism. The same functions can handle different types of data , and are thus generated by the JIT in multiple copies (multiple dispatch) . This must be added to Python by an external module.
- Coroutines, with the same simplicity as Go.
- Metaprogramming. This results from homoiconicity.
- Although by default variables have no type we can give them one by adding annotation such as :: type, for example:
x = 10::int8
- Julia has the equivalent of the C structs with composite types that are also used to declare objects.
type name ... variables ... end
- Types created by the programmer are as fast as language types.
- [a: b] denotes an interval, while Python uses the function range(a, b) and Matlab something similar.
- [a, b] is a list assigned to initialize an array, or a way to concatenate two arrays, or to add an element to an array. Python requires the append method.
- Julia recognizes if the code continues on the next line or not. Python needs some sign in the code.
- Defining a macro to insert complicated expressions in the code is simple:
macro name (expr, expr) ...code ... end
- Julia is designed for distributed processing functions executed in parallel. This allows to perform multiple processings by the same program on the cloud.
- It can uses directly libraries written in C, or Python libraries but with an interface module.
Introduction to the syntax of Julia
The definition begins with the function keyword and ends with end. This is classic, but not optimal, Scriptol simplifies by ending a function with return or return followed by a value.
A function can also be defined by assigning an expression to a name with arguments. For example:
fun(x) = x * 5 println(fun(2))
This should display 10.
What is peculiar to this language is that the return value is the result of the last evaluated expression, unless a return statement is explicitly inserted into the body of the function.
Arrays are passed by reference in arguments and therefore can be changed after a function call.
Operators are functions. You can assign the + symbol to a function f, and make a call to f (x, y), it returns the sum of the two arguments.
The if structure is similar to PHP or Python.
The for loop has the form:
for i = 1:n ... end
This is as easy as Python albeit with a different syntax .
The iteration on a container has the same form that in Scriptol or other modern languages :
for s in [1 , 2, 3] ... end
The while loop is classic :
while a < 5 a += 5 end
There are no other control structure. No until, no switch. Pattern matching is not envisaged by the creators.
Classes and homoiconicity
Classes are composite types in which are nested functions which become their methods.
type maclass function f(x, y) .... end end
Should I use Julia?
I'm really impressed by the possibilities of the language. Go and Dart seem to have been designed in another era. Python is left far behind but has the excuse of having been actually designed in another era.
Julia has all the features of Python and even more, the code is just as easy to write, it has similar libraries in science and seems to be an alternative.
It is not perfect either. Nothing is planned for pattern matching, something that is highly developed in Scala, another modern and innovative language. Objects do not support inheritance or compositions which rules out wide applications.
The documentation is sparse and difficult to understand. Most examples are at command line, that nobody actually uses and it is unreadable.
Error messages are especially a weak point. Most errors within a function are reported on line 1, which is the line where the function begins and are therefore difficult to locate. It will be very difficult to learn programming with this language if no progress is made on this point.
Finally, Julia seems really designed for scientific and difficult to use for general purpose scripting. My experience to implement the sieve of Erasthothenes was laborious. One must practice a little divination. Finding nothing in the documentation on how to insert a comment, I used just in case the Python syntax and was lucky it worked! (Actually C syntax is recognized also).
You must address this language with a pioneer spirit, because you will not find at the moment (February 2014), any help on the Web.
Julia Studio is an free IDE dedicated to the language.
References: The site of Julia. The authors are Jeff Bezanson, Stefan Karpinski, Viral B. Shah, Alan Edelman.