A Gentle Introduction to Julia
A bit of history
2010 was a weird time to be a programmer, on one hand, you had just gotten the first stable release of Haskell, and Rust had just entered the playground. And on the other hand, most production was still going on in Java and C. With C# just having been introduced, Visual Studio had gotten it’s first free edition, which later became a Community edition.
Funnily enough, 10 years later, we’re at a similar point now, with Julia having been stable and strong for a couple of years, another paradigm shift might just be underway. Earlier, languages were domain-specific, C for systems, COBOL for applications, etc. After a couple of generations, we had general-purpose languages like Python, Java, and C++, and the division came between statically typed and dynamically typed. More recently, Rust and Go gave us different ways to go about memory management for parallelization. While Lisp always lurked in the background, having started AI, but now essentially being relegated to just programming emacs.
But still, the generality of today’s languages comes from the tenacity of its user-base. Python, as great as it has become, has constantly evolved and still carries vestiges of its past. It was designed to be easily extensible, readable, and writeable. But it wasn’t a person made to be everything, lest it suffers from the second system effect. Julia is here to do the same thing, but with the exact opposite philosophy.
Now, even if you have no idea what I’m talking about, fret not, this article is about none of the above languages. And yet it’s about all of them! You see, Julia had it’s first release a couple years ago. And while some people might call it an extreme case of the aforementioned second-system syndrome, the Julia community would vehemently say that it’s the exact opposite of it. I shall presently try to persuade you of the same.
Why Julia?
The raison d’etre of Julia is greed. And though it’s not the first time that a language has been based on the throw-in-the-kitchen-sink approach, but it’s the first time that one has succeeded in doing so. Julia intends to bring together all the different features that distinguish particular languages for certain tasks, like the math-like syntax of Haskell, the compactness of Lisp etc, and still stays as fast as C, even though it’s a dynamic language!
Julia also has a robust type system, and it’s flexible enough to support both explicit and implicit declarations, so you get the power of Haskell’s typecasting with the convenience of pythonic type-inference. One final thing, Julia has native support for LaTeX, this is a game-changer, as it lets you essentially embed key-points of your research paper in its implementation itself, and you can even have LaTeX in docstrings.
So, where does Julia stand in the data science domain? Well, unlike python, which has been retrofitted to support machine learning, Julia was meant for math. Hence, it not only has built-in operators for linear algebra but also efficiently leverages GPUs by using JuliaGPU. No more running into Runtime Error 801s, or having to find the correct stack that supports your project as well as GPU manufacturer. Moreover, Julia is a compiled language, making it much faster than all but some flavors of Python. We’ll talk more about Julia in the upcoming code example.
Finally, what about the ecosystem? All major IDEs support Julia. There are Jupyter kernels for Julia, moreover, there’s even a reactive notebook framework for Julia, called Pluto.jl (more on that in a couple of days). Most major python data science libraries like pandas, TensorFlow, etc have Julia variants.
Installation
Now that you’ve heard about the greatness of Julia, how to try it for yourself? Well, if you just want to play around, this page lets you try it in the browser itself without any nagging installations.
If you want to try it on your system locally, this page provides binaries that can be installed directly on linux/macOS/Windows. There are even builds for ARM and Nvidia PTX.
If you’re a vim user, you’ll be glad to know that there’s canonical plugin support. In fact, if you’re an emacs, or IntelliJ or atom, etc user, no worries, the community got you covered right here.
Code samples get ugly in Notions, so hereon out, I’ll be using repl.it, although, in this document, we’ll just go with the norm:
If you are using linux you can install by running following two commands:
sudo apt install snapd
sudo snap install julia --classic
AFter these two commands on terminal. just type julia to terminal and you will get julia prompt as shown in following snapshot:
Hi Julia
No introduction to a programming language is complete without some code. So, here we’ll have a
look at some of the aforementioned strengths of Julia.
Example 1: Big Powerful Printing
First, let me show you how flexible the print command in Julia is:
x="""Hell
o
notice the space
^this space
can even have a blank line 0_0
"""
print(x)
Output:
Hell
o
notice the space
^this space
can even have a blank line 0_0
So, right off the bat, our printing capabilities are more powerful yet convenient than Python, it’s almost as if we were using a scripting language.
Example 2: Type=Static+Dynamic
There are languages in which every variable’s type is determined before runtime, i.e. statically, and there are those in which types are computed during runtime i.e. dynamically. In Julia, a variable doesn’t really have a type, only values have types. Variables are then just a name attached to that
value so as to speak. What I like about statically typed languages is that I can be sure of the type of a variable. I’ll use the interpreter to show how one can achieve similar things in Julia. We’ll work with mundane types like Int64 and Float16 rather than using ones like Real and AbstractFloat.
>> foobar::Int64 Output: ERROR: UndefVarError: foobar not defined Stacktrace:
As expected, an undefined variable doesn’t have a value, and hence can’t have a type. So let’s initialise it and run the same statement. I’ll omit the individual code and output labels this time:
>> foobar=42
Output:
42
>> foobar::Int64
Output:
42
Now let’s check if it’s a float, and if not, let’s rectify it.
>> foobar::Float16 Output: ERROR: TypeError: in Stacktrace: [1] top-level scope >> foobar=42. Output: 42.0 >> foobar::Float16 Output: ERROR: TypeError: in Stacktrace: [1] top-level scope >> foobar::Float64 Output: 42.0
As is common, non-integral rational numbers default to Float64. So, now I’m sure you must be confident that syntactically, Julia is quite similar to Python. Go ahead, install it, and play around with it. This article was purposefully basic. If you already know python, adding Julia to your bag of skills will be easy. Remember, Julia is still a very young language and hence lacks maturity. Thus, for you it’s a good time to learn it, as well as contribute to its development, that way, you’ll be ahead of the curve when it eventually becomes a mainstream language.
Stay posted for updates
Here at Machine Learning Geek, we’re always working on something new to make your journey faster and more fun. For the past couple of days, our team has been revving hard to compare Julia and Python. So, come back soon, as we have a lot of content planned on Julia. In a couple of days, we’ll be talking about Julia datatypes.
If you want to learn python click here