2018/08/24 22:00

# Introduction

Julia is a flexible dynamic language for science and numerical computing with performance comparable to traditional statically typed languages. Perhaps you can use it for the computation of machine learning that has recently received much attention.This blog takes 10 minutes to let you know the language. Then you will be attracted by the language.

# Modules,Variables and Functions

Modules in Julia are separate variable workspaces, i.e. they introduce a new global scope. To load a module you defined, two main keywords can be used: using and import.

source:

module FirstModule

export x, y, z
x() = "x"
y() = "y"
z() = "z"

end

source:

module SecondModule

using Main.FirstModule: x
import Main.FirstModule: y, z

println(x())
println(y())
println(z())

end

output:

x
y
z

A variable, in Julia, is a name associated (or bound) to a value. It's useful when you want to store a value (that you obtained after some math, for example) for later use.

source:

x = 10
y = 23.0
z = x + y
m = sqrt(100)

str = "hello world."

println(z)
println(m)
println(str)

output:

33.0
10.0
hello world.

In Julia, a function is an object that maps a tuple of argument values to a return value.

source:

function area(x, y)
return x * y
end

perimeter = (x, y) -> (x + y) * 2

println(area(5, 10))
println(perimeter(5, 10))


output:

50
30

# Control Flow

source:

function test(x, y)
if x < y
println("x is less than y")
elseif x > y
println("x is greater than y")
else
println("x is equal to y")
end
end

test(5, 10)
test(10, 5)
test(10, 10)

i = 1
while i <= 5
println(i)
global i += 1
end
println("\n")
for i = 1:5
println(i)
end

try
sqrt(-1)
catch err
println(err)
end

output:

x is less than y
x is greater than y
x is equal to y
1
2
3
4
5

1
2
3
4
5
DomainError(-1.0, "sqrt will only return a complex result if called with a complex argument. Try sqrt(Complex(x)).")

# More types: Tuples,Sets,Arrays,Dicts

source:

# Tuple
rect = (10, 20)
println(rect)
println(rect[1])
println("\n")

# Set
x = Set([1,2,3,4])
println(x)

push!(x, 5)
println(x)

push!(x, 2)
println("Cannot insert an existing value 2")
println(x)
println("\n")

# Array
y = Array([1,2,3,4])
println(y)

push!(y, 5)
println(y)

push!(y, 6, 7)
println(y)
println("\n")

# Dict
rect = Dict([("length",10), ("width",20)])
println(rect["length"])
println(rect["width"])

output:

(10, 20)
10

Set([4, 2, 3, 1])
Set([4, 2, 3, 5, 1])
Cannot insert an existing value 2
Set([4, 2, 3, 5, 1])

[1, 2, 3, 4]
[1, 2, 3, 4, 5]
[1, 2, 3, 4, 5, 6, 7]

10
20

# Structs and Methods

Struct keyword is used to build complex data types.

source:

# Struct
struct Box
length::Float64
width::Float64
height::Float64
end

box = Box(10.0, 20.0, 30.0)
println(box.length)

output:

10.0

Method used to implement the concept of Polymorphism.

source:

# Method
function f(x::Float64, y::Float64)
return 2x + 3y
end

println(f(1.0, 2.0))
println(f(1, 2))

output:

8.0
MethodError: no method matching f(::Int64, ::Int64)

Why? Let's  add a function with the same name f.

source:

# Method
function f(x::Float64, y::Float64)
return 2x + 3y
end

function f(x::Int64, y::Int64)
return 2x + 3y
end

println(f(1.0, 2.0))
println(f(1, 2))

output:

8.0
8

After reading the blog, I believe that you have a overall understanding of julia. If you are interested in this language, please visit its official website for more interesting things.
official website: https://julialang.org/

0
0 收藏

0 评论
0 收藏
0