Julia (programming language)

From Wikipedia, the free encyclopedia
Jump to: navigation, search
Julia
Official Julia logo
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[1][2]
Appeared in 2012[3]
Stable release 0.3.1 / 21 September 2014; 30 days ago (2014-09-21)[4][5]
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[6]
OS Linux, OS X, FreeBSD, Windows
License MIT License[1]
Filename extension(s) .jl
Website julialang.org

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.[7][8][9][10] 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[11] and includes efficient libraries for floating point, linear algebra, random number generation, fast Fourier transforms, and regular expression matching.

Language features[edit]

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
Julia dynamic default yes
Common Lisp dynamic opt-in yes (but no dispatch)
Dylan dynamic default partial (no dispatch)
Fortress static default yes

Interaction[edit]

The Julia official distribution include an interactive session shell, called Julia's REPL, which can be used to experiment and test code quickly.[12] 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 ; or ? 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

Examples[edit]

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 julia <filename>.[13]

Hello World[edit]

To print 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.

Flow control[edit]

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

Functions[edit]

The 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)

Metaprogramming[edit]

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.

@assert 1==0

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.

Packages[edit]

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,[14] enabling installation from the Julia prompt:

julia> Pkg.add("PackageName")

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:

using PackageName

Updating Julia's installed packages can also be done using:

julia> Pkg.update()

Examples[edit]

A package is available to call Java from Julia.[15]

Implementation[edit]

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"[16] with "work in progress - several tests are known to fail, and backtraces are not available"[17]).[18][19][20][21][22][23] 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.[24] Development of Julia began in 2009 and an open-source version was publicized in February 2012.[3][25]

References[edit]

External links[edit]