Julia (programming language)
|Paradigm(s)||Multi-paradigm: multiple dispatch ("object-oriented"), procedural, functional, meta|
|Designed by||Jeff Bezanson, Stefan Karpinski, Viral B. Shah, Alan Edelman|
|Developer||Jeff Bezanson, Stefan Karpinski, Viral B. Shah, and other contributors|
|Stable release||0.3.1 / 21 September 2014|
|Preview release||0.3.2-pre and 0.4.0-dev / Updated daily|
|Typing discipline||Dynamic with type inference and optional type annotations|
|Influenced by||MATLAB, Scheme, Lisp, C, Fortran, Wolfram Language, Python, Perl, R, Ruby, Lua|
|OS||Linux, OS X, FreeBSD, Windows|
Julia is a high-level dynamic programming language designed to address the requirements of high-performance numerical and scientific computing while also being effective for general purpose programming. Distinctive aspects of Julia's design include having a type system with parametric types in a fully dynamic programming language, and adopting multiple dispatch as its core programming paradigm. It allows for parallel and distributed computing, and direct calling of C and Fortran libraries without glue code. Julia is garbage collected by default and includes efficient libraries for floating point, linear algebra, random number generation, fast Fourier transforms, and regular expression matching.
According to the official web site, the main features of the language are:
- Multiple dispatch: providing ability to define function behavior across many combinations of argument types
- Dynamic type system: types for documentation, optimization, and dispatch
- Good performance, approaching that of statically-compiled languages like C
- Built-in package manager
- Lisp-like macros and other metaprogramming facilities
- Call Python functions: use the PyCall package
- Call C functions directly: no wrappers or special APIs
- Powerful shell-like capabilities for managing other processes
- Designed for parallelism and distributed computation
- Coroutines: lightweight "green" threading
- User-defined types are as fast and compact as built-ins
- Automatic generation of efficient, specialized code for different argument types
- Elegant and extensible conversions and promotions for numeric and other types
- Efficient support for Unicode, including but not limited to UTF-8
Julia draws significant inspiration from various dialects of Lisp, including Scheme and Common Lisp, and it shares many features with Dylan – also a multiple-dispatch-oriented dynamic language – and Fortress, another numerical programming language with multiple dispatch and a sophisticated parametric type system. While CLOS adds multiple dispatch to Common Lisp, the addition is opt-in: only user-defined functions explicitly declared to be generic can be extended with new multimethods.
In Julia, Dylan and Fortress, on the other hand, this extensibility is the default and the system's built-in functions are all generic and extensible. In Dylan, multiple dispatch is as fundamental as it is in Julia: all user-defined functions and even basic built-in operations like
+ are generic. Dylan's type system, however, does not fully support parametric types, which are more typical of the ML lineage of languages. By default, CLOS does not allow for dispatch on Common Lisp's parametric types; such extended dispatch semantics can only be added as an extension through the CLOS Metaobject Protocol. By convergent design, Fortress also features multiple dispatch on parametric types; unlike Julia, however, Fortress is statically rather than dynamically typed, with separate compilation and execution phases. This matrix of language features is summarized in the following table:
|Language||Type system||Generic functions||Parametric types|
|Common Lisp||dynamic||opt-in||yes (but no dispatch)|
|Dylan||dynamic||default||partial (no dispatch)|
The Julia official distribution include an interactive session shell, called Julia's REPL, which can be used to experiment and test code quickly. The following fragment represents a sample session on the REPL:
julia> 1 + 2 * 3 7
The REPL gives user access to the system shell and to help mode, by pressing
? after the prompt (preceding each command), respectively:
shell> mkdir test shell> ls test help?> print INFO: Loading help data... Base.print(x) Write (to the default output stream) a canonical (un-decorated) text representation of a value if there is one, otherwise call "show". The representation used by "print" includes minimal formatting and tries to avoid Julia-specific details.
The REPL also keeps the history of commands, even between sessions.
You can see the compiled assembly code for any just typed in function; including in-built functions such as the operator + that is just a function through syntactic sugar, but then it may not have been run first:
julia> code_native(+, (Float64, Float64)) Warning: Unable to find function pointer ERROR: no method found for the specified argument types in code_native at reflection.jl:159 julia> 1.0 + 2.0 3.0 julia> code_native(+, (Float64, Float64)) .text Filename: float.jl Source line: 120 push RBP mov RBP, RSP Source line: 120 addsd XMM0, XMM1 pop RBP ret
The following examples can be tested inside the Julia's interactive section, or saved into a file with a
.jl extension, and run from the command line by typing
Hello World on command line:
julia> println("Hello World") Hello World
julia> @printf("Hello %d %d %d\n", 1, 2, 3) Hello 1 2 3
Strings, in Julia, live inside double quotes (and can span many lines for both examples), and single quotes are reserved to characters (much like in the C language). The @ in the @printf form means it is a macro that generates code, here the literal string printed, at compile time.
In Julia, if-elseif-else statements can be used to select an action based on the truth value of an expression (in the following case, the
x > 10 expression), that is, if that expression is true or false:
if x > 10 println("x is totally bigger than 10.") elseif x < 10 # This elseif clause is optional. println("x is smaller than 10.") else # The else clause is optional too. println("x is indeed 10.") end
For looping while an expression is true, there is the while statement:
# While loops loop while a condition is true x = 0 while x < 4 println(x) x += 1 # Shorthand for x = x + 1 end
Julia also has the for statement:
for i = 1:10 println(i) end
One can also use For loops to iterate over a collection of elements, like an array:
for animal in ["dog", "cat", "mouse"] println("$animal is a mammal") end
function statement can be used to create functions:
function p(x, y) return 2x^2 + y end
As functions always return the value from the last statement run the
return keyword is optional in this case. Functions can also simply be defined this way:
p(x, y) = 2x^2 + y
Functions need not be mathematical, they can have side effects, e.g. print out stuff or change parameters but then an ! is by convention added to the functions name.
Functions can also return more than one value (e.g. tuples):
tup(x) = (x + 1, x + 2, x + 3)
Julia, like Lisp, represents its own code in memory using a user-accessible data structure, thereby allowing programmers to both manipulate and generate code which the system can evaluate. This makes complex code generation and transformation far simpler than in systems without this feature. A relatively simple example of the latter is an assert macro that will raise an error if an expression is false:
macro assert(ex) :($ex ? nothing : error("Assertion failed: ", $(string(ex)))) end
Notice that while the argument to a function are typically variables, the arguments to macros are expressions. Given an expression, e.g.
the macro generates the new expression
1==0 ? nothing : error("Assertion failed: ", "1==0")
where the original expression has been spliced into the condition slot of the ternary operator and has been converted to a string for the error message. Now if the original expression evaluates as true, nothing happens, where if the expression evaluates as false, an error is raised.
In the Julia packaging system each package is a Git repository that can be stored in any publicly accessible location. A master package listing that includes package dependency information is maintained in METADATA.jl, enabling installation from the Julia prompt:
Packages are typically written in Julia but can include both binaries and code written in other languages, which can be automatically compiled at package install time by the package manager. To load the installed package into Julia, one can run:
Updating Julia's installed packages can also be done using:
A package is available to call Java from Julia.
Julia's core is implemented in C and C++, its parser in Scheme, and the LLVM compiler framework is used for just-in-time generation of 64-bit or 32-bit optimized machine code depending on platform or user override. Current support is for newer x86 or older i386 processors and in 0.4.0-dev, 32-bit ARMv7 ("Experimental and early support" with "work in progress - several tests are known to fail, and backtraces are not available"). The standard library is implemented in Julia itself, using Node.js's libuv library for efficient, cross-platform I/O. The most notable aspect of Julia's implementation is its speed, which is often within a factor of two relative to fully optimized C code. Development of Julia began in 2009 and an open-source version was publicized in February 2012.
- "LICENSE.md". GitHub.
- "Contributors to JuliaLang/julia". GitHub.
- "Why We Created Julia". Julia website. February 2012. Retrieved 7 February 2013.
- "The Julia Language" (official website).
- Bryant, Avi (15 October 2012). "Matlab, R, and Julia: Languages for data analysis". O'Reilly Strata.
- Krill, Paul (18 April 2012). "New Julia language seeks to be the C for scientists". InfoWorld.
- Finley, Klint (3 February 2014). "Out in the Open: Man Creates One Programming Language to Rule Them All". Wired.
- "Suspending Garbage Collection for Performance...good idea or bad idea?".
- Julia REPL documentation
- Learn Julia in Y Minutes
- "METADATA.jl" (central package listing for Julia)
- ARM test failures
- ARM nightlies
- Segfault using Pkg on ARM
- build changes for ARM #7662
- Port to ARM #3134
- "Julia: A Fast Dynamic Language for Technical Computing" (PDF). 2012.
- Gibbs, Mark (9 January 2013). "Pure and Julia are cool languages worth checking out". "Gear head". Network World (column). Retrieved 7 February 2013.