Overview for julia

Global variables within loop in Julia

Julia has unusial definition of global variables within loop cycles.

For example, in other languages, it's possible to use outer variable and change it inside for-loop construction (JavaScrip code):

JavaScript code

var i;
for (i = 0;i < 12;i++) {
  i+=1;
} 
console.log(i);

This code will result in:

12

Python code

y = 0;
for x in range(0,5):
    y += 1

print(y)

output:

5

Read more

Written by Administrator on Thursday May 14, 2020

How to count files in current directory in Julia?

If you plan to count files in current directory in Julia, you can just run following code:

files_and_dirs = readdir(pwd())  # reading files and directory

function count()
    global num =0
    for i in files_and_dirs
        if isfile(i)
            num +=1
        end
    end
end

count()

println(num)

Read more

Written by Administrator on Thursday May 14, 2020

How to get file's extension in Julia

Working with files in every programming language is almost everyday routine. While writing scripts and programs in Julia, sometimes we need to choose files with certain extension. Julia offers very simple, fast and powerful tools for operation with strings.

In order to get (or print) file's extension in Julia, we don't even need to use regular expressions (which surely will took time to learn). Another question is, that not all files have extensions. In this case we have nothing to do, because in this case file's extension will be an empty string. Next question is files with complex extensions, for example .tar.gz. We are not going to solve those types, because I cannot imagine a situation, when there's need to change .tar.gz extension. That's why, let's focus in files, which have most common extensions (1,2,3,4 - characters), for example: .c , .sh or .zip.

In our example we will combine several string operations to get the final result.

Technique is very simple:

1) Let's take on a filename with extension like a string:

julia> filename = "the_most_interesting_movie_ever.avi"

2) We are looking for a last occurance of a dot (.) and find the position of this dot (meaning, on which position stands that dot in a row of characters a filename consist of):

julia> findlast(isequal('.'),filename)
32

No we know, that dot has the 32-nd position in a string "the_most_interesting_movie_ever.avi"

Read more

Written by Administrator on Thursday May 14, 2020

Functions with optional positional arguments in Julia (explanation with examples)

Sometimes we need a function, which requires several arguments. It's normal situation and this is the way functions work normally almost in all programming languages. Optional arguments will make your code easier and more powerful with decreasing a number of functions you create.

Example No.1 : Calculating engine consumption

For example, let's create a function, which returns fuel consumption depending on engine volume (cubic centimeters = cc).

function ShowConsumption(cc)
    return round(cc/170)          # rounding float-number result to the 2nd decimal
end

So, when we have to know a consumption of an engine of 1950cc, we will provide a parameter 1950:

julia> println(ShowConsumption(1950))
11.47

Great, and what we have to do in case we have to add new functionality: for example, Consumption in winter. What we will do? Sure, we will write a new function ShowConstumptionInWinter(cc), but there's a more clever way to solve a problem: we can use optional arguments for our function.

We will leave original function ShowConsumption and add 1 optional argument, which will be used only in case we want our function calculate something different. Let's take we know that for winter consumption is as twice as big as in summer and it means that function must double normal consumption. It's better to show in on example:

Read more

Written by Administrator on Monday May 11, 2020

Infinite recursion and StackOverflowError in Julia

In order to evoke StackOverflowError in Julia it's enough to write a function, which result in infinite recursion. Let's see, how it works:

julia>

function foo(arg)
           if arg>1
               println("x")
           else
               foo(arg)
           end
       end

And now let's call foo() function with an argument:

julia> foo(1)

Read more

Written by Administrator on Sunday May 10, 2020

Dictionaries in Julia - Beginner's tutorial [with examples]

Dictionary is a kind of Data Structure in many programming languages and it is a part of Julia programming language as well. Let's create a simple example for understand, how Dictionaries work in Julia.

Imagine we have a Car Store and we're selling different cars. Our plan is to create a list of cars with possibility of looking for a price by giving a car's name.

Creating a Dictionary

Creating Dict variable, which will represent our pricelist. As a key we will store the name of a product and as a value we will store its price (let's say in US Dollars):

julia> Pricelist = Dict("Honda" => 12000, "Fiat" => 9800, "Ford" => 11000, "Lambo" => 175000)

You see, the core of a Dictionary is a pair of two values. In our case a name of a car is a key and car's price is a value.

Read more

Written by Administrator on Sunday May 10, 2020