As in many scripting languages, functions can be quite compact in Julia:

function f ( x , y )

x + y

end

On top of that there is the short notation f (x, y) = x + y. By default, Julia returns the value of the last expression in the function. If in doubt, returnallows you to return a specific value. Several return values are also possible, which Julia packs into a corresponding tuple and then returns this.

Tuples complement arrays already known from other languages and can store arbitrary values: x = (2.3, Hello, 5 + 4). The access is made with the parenthesis notation, for example , x [2] returns hello.

Functions treated Julia as first-class objects. They can therefore be assigned to variables, passed as arguments and returned as return values. Anonymous functions are also possible:

function ( x )

2x + 3

end

For them there is also a short notation, which for the example is x -> 3x + 2. As in other languages, anonymous functions are mainly used to pass them directly as arguments to another function.

Wine project fears problems with 64-bit Ubuntu

Good arguments

The arguments of functions can be pre-populated with standard values: function f (a, b = 2). That would be enough to call f (1) . In addition one may give names to the arguments. This emphasizes the meaning, in addition one may exchange their order. These so-called keyword arguments stand behind a semicolon:

function male ( x , y ; brush = fine , thickness = 1 ) …

male ( 1 , 2 , thickness = 2 )

If desired, a function can also accept any number of arguments that Julia then makes available within the function in a tuple ( Varargs Functions ). Even the operators like + , * or – are all functions for Julia – 1 + 2 is just an alternative notation for + (1,2).

Always in the river

Conditional statements are made using the if-else construct known from many other languages, where the condition must always be true or false :

if x > 0

Positive number

else

Negative number

end

Julia offers only a while and a for loop. The latter iterates over a sequence of numbers or the elements in a container. In the following example, it outputs the numbers from 1 to 5:

for i = 1 : 5

println ( i )

end

Loops can be aborted with break , continue immediately starts the next loop pass. Two nested loops can pull developers together to one:

for i = 1 : 2 , j = 3 : 4

println ( ( i , j ) )

end

Julia knows exceptions, which can throw functions with a throw throw. Similar to Java, the environment catches the errors with a try and catch block.

Typical

Among other things, Julia knows the intertext types Int8, Int16 and Int32. Occasionally one would like to write code that can handle all types of integers. This is helped by the Abstract Types, which together form a hierarchy, the Type Graph. An abstract type is, for example, the type Number, which in turn stands for all numbers. The function foo (a :: Numbers)would therefore accept all numbers as arguments.

At the end of the hierarchy are the primitive types, which simply occupy a given number of bits in memory. If necessary, you can define abstract types and primitive types yourself. In the following example, the abstract data type MyNumber is a subtype of Number

abstract type mynumber <: Number end

In addition, Julia provides even more interesting data type concepts. In this way, Union {}can be used to combine several types in one and thus implement the data type String orNumber. Developers can also query and compare the type in the program code. For example, isa () checks to see if an object matches a particular type- for example , isa (1, int)would be true.

Windows 10 annoys users with ghost updates for uninstalled apps

Multiple dispatch

In Julia several functions with the same name but different arguments may exist. When calling up the function, Julia automatically selects the appropriate one. In the following example, there are two times a foo () function:

function foo ( a :: Numbers ) …

function foo ( a :: Float16 ) …

Both perform a calculation with a number a. If you later call foo () with a number of the type Float16, Julia would automatically resort to the second function, in all other cases choose the first one. In this way the programming language realizes polymorphism.

The individual variants of foo () designate Julia as methods. The term is thus interpreted somewhat differently than in object-oriented languages, where methods are usually part of a class. The selection of the appropriate method is generally called Dispatch.

Julia is always guided by all the arguments passed and their types, as well as the Type Graph. For object-oriented languages, however, the choice is usually based on the class to which the object belongs. Julia’s approach is therefore also referred to as multiple dispatch. This is especially useful for mathematical tasks. The best known example is the function + , which has to do an addition on all possible data types. In Julia, developers can + simply add suitable variants for their own data types. It is even possible to specify rules according to which Julia should translate mixed types (such as the addition of an integer with a floating-point number) into a separate type ( promotion ).

class replacement

Unlike object-oriented languages, Julia knows no classes. Similar to C, however, you can use the keyword struct to group multiple variables into a new data type:

struct person

Surname

age :: UInt8

end

Julia calls these composite data types composite types. The person created in the example can be used to create an object with a function call:

hans = person ( Hans Meier , 35 )

Hans is thus of the type person. The called function designates Julia as a constructor. Similar to the counterpart in object-oriented languages of the same name, you can overload the contructor automatically provided by Julia by simply defining other suitable methods.

By default, the elements of hans can be read via the dot operator ( a = hans.alter ), but they can not be changed. This is only possible if the struct is explicitly preceded by the keyword mutable. Julia always manages the corresponding objects on the heap.

Generic

Composite types can be parameterized. Other languages know the concept as generics. These, for example, are useful if the actual data type is still unknown:

struct point { T }

x :: T

y :: T

end

a = Point { UInt8 } ( 2 , 3 )

Only when creating an object decides which data stores “Point”. In the example, the type of a is then Point {UInt8}. Similarly, functions can also be parameterized.

parallel processing

Julia can perform several functions in parallel. These so-called tasks can be paused and continue later. Data is exchanged via so-called channels. These are First-In-First-Out (FIFO) queues, into which multiple tasks can simultaneously write and read data:

function count ( c :: Channel )

for number = 1 : 10

put ! ( c , number )

end

end ;

chnl = channel ( count ) ;

a = take ! ( chnl )

println ( a )

count is the task that pushes the corresponding value into channel c via put! (). After each put! () Julia stops the function count () and passes the control back to the calling code. The Channel Constructor Channel (count) creates a task bound to the channel. take! () reads a date from the channel and ensures that the producer () continues to run.

Internally, Julia realizes the tasks as coroutines. In addition, version 1.1 of the programming language also provides experimental support for multithreading. On the other hand, support for distributed computing is stable. The Julia interpreter runs several times locally or on remote computers. The programmer can then selectively call individual Julia functions on the remote computers.

Metaprogramming

Similar to Lisp, the Julia program code itself can change. The programming language provides suitable functions for this purpose. Added to this are the macros based on Lisp. These helpers take several arguments that they incorporate into an expression:

macro hello ( x )

return : ( println ( hello , $ x ) )

end

@ hello ( Paul )

@hello replaces Julia with println (hello, Paul). Julia compiles the code given by Makro even before the complete program starts. The mulitple dispatch principle also applies to macros, so there can be several method definitions.

Many other little things

In addition to the presented constructs Julia offers many more interesting concepts. For example, arrays can be created extremely flexibly via so-called comprehensions. Anyone who knows the set definition from mathematics should get warm quickly with the syntax. The following example provides the array [4,5]:

x = [ a + b for a = 1 : 2 , b = 3 ]

This notation can also be used in a so-called generator expression. sum (n for n = 1:10)calculates the sum of all numbers from 1 to 10.

Particularly interesting for matrix calculations is the function broadcast (), which applies a function passed to it to all elements of an array. If necessary, the function adjusts the dimensions of the participating arrays without occupying additional memory.

Furthermore, Julia offers a built-in documentation system: If you place a string alone in front of a function definition or other corresponding code, Julia interprets it as documentation (so-called docstrings). Among other things, these help texts can evaluate and search the command line version of Julia as well as the Juno IDE.

Finally, Julia code can be called up via appropriate interfaces from C programs. Conversely, programmers can start external programs from the Julia code and call C and Fortran code. In this way, numerous C libraries can be integrated and used further.

Julia combines the advantages of a scripting language with efficient and fast running binaries. Especially in mathematical calculations, the processing of data and in science, it offers an interesting alternative to Python, R and Matlab. Who wants to get into Julia, should consult the official documentation. It discusses all aspects of the language in great detail and provides numerous tips for the daily work and the development of Julia programs.