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|
|Stable release||0.2.1 / 11 February 2014|
|Typing discipline||Dynamic with optional type annotations, and type inference|
|Influenced by||MATLAB, Scheme, Lisp, C, Fortran, Wolfram Language, Python, Perl, R, Ruby|
|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. Unusual 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 includes efficient libraries for floating point, linear algebra, random number generation, fast Fourier transforms, and regular expression matching.
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 machine code for x86(-64) with work being done to get it working on ARM. The standard library is implemented in Julia itself, using the 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 of fully optimized C code. Development of Julia began in 2009 and an open-source version was publicized in February 2012.
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 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, you can type:
Strings, in Julia, lives inside double quotes, and single quotes are reserved to characters (much like C)
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 of 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 if 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
You 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
You can use the
function statement to create functions:
function add(x, y) return x + y end
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.
$ julia _ _ _(_)_ | A fresh approach to technical computing (_) | (_) (_) | Documentation: http://docs.julialang.org _ _ _| |_ __ _ | Type "help()" to list help topics | | | | | | |/ _` | | | | |_| | | | (_| | | Version 0.3.0-prerelease+2954 _/ |\__'_|_|_|\__'_| | |__/ | julia> println("Hello World") "Hello World" julia> 1 + 1 2
The REPL gives user access to the system shell and to help mode, by pressing
? after the prompt, 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.
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, you can run:
Updating Julia's installed packages can also be done using:
A package is available to call Java from Julia.
- "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.
- Port to ARM #3134
- "Julia: A Fast Dynamic Language for Technical Computing" (PDF). 2012.
- "Why We Created Julia". Julia website. February 2012. Retrieved 7 February 2013.
- Gibbs, Mark (9 January 2013). Pure and Julia are cool languages worth checking out. "Gear head". Network World (column). Retrieved 7 February 2013.
- Learn Julia in Y Minutes
- Julia REPL documentation
- "METADATA.jl" (central package listing for Julia)