A Beginner’s Look at Julia

Over the past month or so, I’ve been playing with a new scientific programming language called ‘Julia‘, which aims to be a high-level language with performance approaching that of C. With that goal in mind, Julia could be a replacement for the ‘multi-language’ problem of needing to move between R, Python, MATLAB, C, Fortran, Scala, etc. within a single scientific programming project.  Here are some observations that might be helpful for others looking to get started with Julia.

Get used to ‘Git’ and ‘make’

While there are pre-built binaries for Julia, due to the rapid pace of development, it’s best to build Julia from source. To be able to keep up with the literally dozen code changes per day, you can clone the Julia GitHub repository to your local machine. If you use one of the GitHub GUI’s, this is as easy as hitting the ‘Sync Branch’ button to receive all of the newest code updates.

To install Julia, you need to compile the code. The instructions for each supported operating system are listed on the Julia GitHub page. For Mac users, use Terminal to navigate to the directory where you cloned Julia, then run the following command, where ‘n’ refers to the number of concurrent processes you want the compiler to use:

make -j n

I use 8 concurrent processes on a 2013 MacBook Pro and it works pretty well. Certainly much faster than a single process. Note that the first time you run the ‘make’ command, the build process will take much longer than successive builds, as Julia downloads all the required libraries needed. After the first build, you can just run the ‘make’ command with a single process, as the code updates don’t take very long to build.

Package management is also done via GitHub. To add Julia packages to your install, you use the Pkg.add() function, with the package name in double-quotes.

Julia code feels very familiar

Text file import

Although the Julia documentation makes numerous references to MATLAB in terms of code similarity, Julia feels very familiar to me as an R and Python user. Take reading a .csv file into a dataframe and finding the dimensions of the resulting object:
In each language, the basic syntax is to call a ‘read’ function, specify the .csv filename, then the defaults of the function read in a basic file. I also could’ve specified other keyword arguments, but for purposes of this example I kept it simple.

Looping

Looping in Julia is similar to other languages. Python requires proper spacing for each level of a loop, with a colon for each evaluated expression. And although you generally don’t use many loops in R, to do so requires using parenthesis and brackets.

If you’re coming from a Python background, you can see that there’s not a ton of difference between Python looping into a dictionary vs. Julia. The biggest differences are the use of the ‘end’ control-flow word and that Julia doesn’t currently have the convenience “Counter” object type. R doesn’t natively have a dictionary type, but you can add a similar concept using the hash package.

Vectorization

While not required to achieve high performance, Julia also provides the functional programming construct of vectorization and list comprehensions. In R, you use the ‘apply’ family of functions instead of loops in order to apply a function to multiple elements in a list. In Python, there are the ‘map’ and ‘reduce’ functions, but there is also the concept of list comprehensions. In Julia, both of the aforementioned functionalities are possible.

In each case, the syntax is just about the same to apply a function across a list/array of numbers.


A small, but intense community

One thing that’s important to note about Julia at this stage is that it’s very early. If you’re going to be messing around with Julia, there’s going to be a lot of alone-time experimenting and reading the Julia documentation. There are also several other resources including a Julia-Users Google group, Julia for R programmers, individual discussions on GitHub in the ‘Issues’ section of each Julia package, and a few tutorials floating around (here and here).

Beyond just the written examples though, I’ve found that the budding Julia community is very helpful and willing in terms of answering questions. I’ve been bugging the hell out of John Myles White and he hasn’t complained (yet!), and even when code issues are raised through the users group or on GitHub, ultimately everyone has been very respectful and eager to help. So don’t be intimidated by the fact that Julia has a very MIT and Ph.D-ness to it…jump right in and migrate some of your favorite code over from other languages.

While I haven’t moved to using Julia for my everyday workload, I am getting facility to the point where I’m starting to consider using Julia for selected projects. Once the language matures a bit more, JuliaStudio starts to approach RStudio in terms of functionality, and I get more familiar with the language in general, I can see Julia taking over for at least one if not all of my scientific programming languages.

Comments

  1. anonymous says:

    Randy, nice post. Could you comment a bit more on “Julia could be a replacement for the ‘multi-language’ problem of needing to move between R, Python, MATLAB, C, Fortran, Scala, etc. within a single scientific programming project”

    I know nothing about Julia, and so wasn’t clear on whether Julia could incorporate code from other languages or if you think that the functionality provided by these languages can be replaced by Julia. The former seems like something R at least tries to do, and the latter seems unlikely although I have been hearing great things indeed about Julia.

    • Randy Zwitch says:

      Thanks for the comment!

      The ‘multi-language’ problem deals with the trade-off between high-level, dynamically typed languages such as R and Python vs. performance. In many cases, people will prototype in one language that’s easy (i.e. dynamic), but then when they move to production, the coding of the model/algorithm is done in another language like C or Java to get better performance (because the code is compiled).

      The goal behind Julia (and I’m not one of the dev’s, so this is my opinion only) is that the performance could get close enough to C that you don’t need to use multiple languages. Doing interactive analysis in Julia should be easy because it’s a high-level language, but also fast because of the just-in-time compiler. You can also call C code easily in Julia, so you don’t have to have a second workflow just for C.

      Of course, Julia is at a very young stage right now, so there aren’t nearly as many packages available as R or Python. But the functionality is quickly being written for what I would call the “core” functionalities (accessing databases, core statistical techniques/machine learning, strong mathematical support, etc.), so it will be interesting to see how things look in 2014 and beyond.

  2. Great article! In addition to the *pply family of functions, R’s support for implicit vectorization simplifies the process of applying a function over an atomic vector:

    cube <- function(x) x*x*x; cube(seq(1, 100))
    # or
    seq(1,100)^3

    I'm wondering if Julia supports similarly vectorized functions?

  3. As a community resource, I would also like to point out the Google+ group, which can function as a little more social meeting place than the anon feeling mailinglists (at least you have the faces of the ones you discuss with):

    Visit: http://gplus.to/julialang

  4. Brian D. Martin says:

    I have tried to install Julia twice on my machine. I get no error messages, but I can’t run “julia” from a command line. Is there community forum to find out if there are bugs in the download / install process? Thank you.

  5. John Lynch says:

    Nice article Randy,

    As another relatively new Julia user can I leave some suggestions for the code?

    - in Loops you use 2 space tabulation for Python and 4 for Julia. Reversing that would reflect more common Pythonic and Julian practice.

    - in Loops you have a very complete line to increment the dictionary. A terser term_freq[word] += 1 should also work I think.

    - in Vectorization you use an array [1:100] to iterate over but 1:100 without the [] provides a more efficient form.

    Any of your readers contemplating exploring Julia from Python might find the reference card I modeled from Python quite useful: https://github.com/john9631/JuliaDocs

Trackbacks

  1. [...] Randy Zwitch has a tutorial on the scientific programming language Julia. [...]

Leave a Reply