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 FirstModuleexport x, y, zx() = "x"y() = "y"z() = "z"end
source:
module SecondModuleusing Main.FirstModule: ximport Main.FirstModule: y, zprintln(x())println(y())println(z())end
output:
xyz
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 = 10y = 23.0z = x + ym = sqrt(100)str = "hello world."println(z)println(m)println(str)
output:
33.010.0hello 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 * yendperimeter = (x, y) -> (x + y) * 2println(area(5, 10))println(perimeter(5, 10))
output:
5030
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") endendtest(5, 10)test(10, 5)test(10, 10)i = 1while i <= 5 println(i) global i += 1endprintln("\n")for i = 1:5 println(i)endtry sqrt(-1)catch err println(err)end
output:
x is less than yx is greater than yx is equal to y1234512345DomainError(-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:
# Tuplerect = (10, 20)println(rect)println(rect[1])println("\n")# Setx = 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")# Arrayy = Array([1,2,3,4])println(y)push!(y, 5)println(y)push!(y, 6, 7)println(y)println("\n")# Dictrect = Dict([("length",10), ("width",20)])println(rect["length"])println(rect["width"])
output:
(10, 20)10Set([4, 2, 3, 1])Set([4, 2, 3, 5, 1])Cannot insert an existing value 2Set([4, 2, 3, 5, 1])[1, 2, 3, 4][1, 2, 3, 4, 5][1, 2, 3, 4, 5, 6, 7]1020
Structs and Methods
Struct keyword is used to build complex data types.
source:
# Structstruct Box length::Float64 width::Float64 height::Float64endbox = Box(10.0, 20.0, 30.0)println(box.length)
output:
10.0
Method used to implement the concept of Polymorphism.
source:
# Methodfunction f(x::Float64, y::Float64) return 2x + 3yendprintln(f(1.0, 2.0))println(f(1, 2))
output:
8.0MethodError: no method matching f(::Int64, ::Int64)
Why? Let's add a function with the same name f.
source:
# Methodfunction f(x::Float64, y::Float64) return 2x + 3yendfunction f(x::Int64, y::Int64) return 2x + 3yendprintln(f(1.0, 2.0))println(f(1, 2))
output:
8.08
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: