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
Appeared in 2012
Stable release 0.2.1 / 11 February 2014; 4 months ago (2014-02-11)
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
License MIT License
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.[1][2][3][4] 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.[5] 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.[6] Development of Julia began in 2009 and an open-source version was publicized in February 2012.[7][8]

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

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>.[9]

Hello World[edit]

To print Hello World on command line, you can type:

println("Hello World")

Strings, in Julia, lives inside double quotes, and single quotes are reserved to characters (much like C)

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

Functions[edit]

You can use the function statement to create functions:

function add(x, y)
    return x + y
end

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.

Interaction[edit]

The Julia official distribution include a interactive session shell, called Julia's REPL, which can be used to test code quickly.[10] The following fragment represents a sample section on the REPL:

$ 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 ; or ? 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.

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,[11] 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, you 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.[12]

References[edit]

  1. ^ "The Julia Language" (official website). 
  2. ^ Bryant, Avi (15 October 2012). "Matlab, R, and Julia: Languages for data analysis". O'Reilly Strata. 
  3. ^ Krill, Paul (18 April 2012). "New Julia language seeks to be the C for scientists". InfoWorld. 
  4. ^ Finley, Klint (3 February 2014). "Out in the Open: Man Creates One Programming Language to Rule Them All". Wired. 
  5. ^ Port to ARM #3134
  6. ^ "Julia: A Fast Dynamic Language for Technical Computing" (PDF). 2012. 
  7. ^ "Why We Created Julia". Julia website. February 2012. Retrieved 7 February 2013. 
  8. ^ Gibbs, Mark (9 January 2013). Pure and Julia are cool languages worth checking out. "Gear head". Network World (column). Retrieved 7 February 2013. 
  9. ^ Learn Julia in Y Minutes
  10. ^ Julia REPL documentation
  11. ^ "METADATA.jl" (central package listing for Julia)
  12. ^ http://aviks.github.io/JavaCall.jl/

External links[edit]