سلام.
مطلب زیر را توی وبلاگ http://julialang.blogsky.com دیدم. خودم که می خواستم جولیا را برای اولین بار یاد بگیرم و شرع کنم. به این وبلاگ برخوردم و این مطلب را دیدم. برای شروع کار راه انداز هست. همه ی دستور هایی که می بینید ممکن هست به دردتون نخوره پس به راحتی از روش رد بشید و فقط اونهایی که به دردتون می خوره را ببینید.
'
after the text. They can also be nested.
=
3
3.2
2 + 1im
2//3
1 + 1
8 - 1
10 * 2
35 / 5
5 / 2
div(5, 2)
5 35
2 ^ 2
12 % 10
(1 + 3) * 2
~2
3 & 5
2 | 4
2 $ 4
2 >>> 1
2 >> 1
2 << 1
bits(12345)
bits(12345.0)
true
false
!true
!false
1 == 1
2 == 1
1 != 1
2 != 1
1 < 10
1 > 10
2 <= 2
2 >= 2
1 < 2 < 3
2 < 3 < 2
'a'
"This is a string"[1]
str = "2 + 2 = $(2 + 2)"
@printf "%d is less than %f" 4.5 5.3
println("I'm Julia. Nice to meet you!")
some_var = 5
some_var
try
some_other_var
catch e
println(e)
end
SomeOtherVar123! = 6
ρ = 8
2 * π
a = Int64[]
b = [4, 5, 6]
b[1]
b[end]
matrix = [1 2; 3 4]
push!(a,1)
push!(a,2)
push!(a,4)
push!(a,3)
append!(a,b)
pop!(b)
push!(b,6)
a[1]
a[end]
shift!(a)
unshift!(a,7)
arr = [5,4,6]
sort(arr)
sort!(arr)
try
a[0]
a[end+1]
catch e
println(e)
end
a = [1:5]
a[1:3]
a[2:end]
arr = [3,4,5]
splice!(arr,2)
b = [1,2,3]
append!(a,b)
in(1, a)
length(a)
tup = (1, 2, 3)
tup[1]
try
tup[1] = 3
catch e
println(e)
end
length(tup)
tup[1:2]
in(2, tup)
a, b, c = (1, 2, 3)
d, e, f = 4, 5, 6
(1,) == 1
(1) == 1
e, d = d, e
empty_dict = Dict()
filled_dict = Dict("one"=> 1, "two"=> 2, "three"=> 3)
filled_dict["one"]
keys(filled_dict)
values(filled_dict)
in(("one" => 1), filled_dict)
in(("two" => 3), filled_dict)
haskey(filled_dict, "one")
haskey(filled_dict, 1)
try
filled_dict["four"]
catch e
println(e)
end
get(filled_dict,"one",4)
get(filled_dict,"four",4)
empty_set = Set()
filled_set = Set([1,2,2,3,4])
push!(filled_set,5)
in(2, filled_set)
in(10, filled_set)
other_set = Set([3, 4, 5, 6])
intersect(filled_set, other_set)
union(filled_set, other_set)
setdiff(Set([1,2,3,4]),Set([2,3,5]))
some_var = 5
if some_var > 10
println("some_var is totally bigger than 10.")
elseif some_var < 10
println("some_var is smaller than 10.")
else
println("some_var is indeed 10.")
end
for animal=["dog", "cat", "mouse"]
println("$animal is a mammal")
end
for animal in ["dog", "cat", "mouse"]
println("$animal is a mammal")
end
for a in ["dog"=>"mammal","cat"=>"mammal","mouse"=>"mammal"]
println("$(a[1]) is a $(a[2])")
end
for (k,v) in ["dog"=>"mammal","cat"=>"mammal","mouse"=>"mammal"]
println("$k is a $v")
end
x = 0
while x < 4
println(x)
x += 1
end
try
error("help")
catch e
println("caught it $e")
end
function add(x, y)
println("x is $x and y is $y")
x + y
end
add(5, 6)
function varargs(args...)
retu args
end
varargs(1,2,3)
tuple([1,2,3])
tuple([1,2,3]...)
x = (1,2,3)
tuple(x)
tuple(x...)
function defaults(a,b,x=5,y=6)
retu "$a $b and $x $y"
end
defaults('h','g')
defaults('h','g','j')
defaults('h','g','j','k')
try
defaults('h')
defaults()
catch e
println(e)
end
function keyword_args(;k1=4,name2="hello")
retu ["k1"=>k1,"name2"=>name2]
end
keyword_args(name2="ness")
keyword_args(k1="mine")
keyword_args()
function all_the_args(normal_arg, optional_positional_arg=2; keyword_arg="foo")
println("normal arg: $normal_arg")
println("optional arg: $optional_positional_arg")
println("keyword arg: $keyword_arg")
end
all_the_args(1, 3, keyword_arg=4)
function create_adder(x)
adder = function (y)
retu x + y
end
retu adder
end
(x -> x > 2)(3)
function create_adder(x)
y -> x + y
end
function create_adder(x)
function adder(y)
x + y
end
adder
end
add_10 = create_adder(10)
add_10(3)
map(add_10, [1,2,3])
filter(x -> x > 5, [3, 4, 5, 6, 7])
[add_10(i) for i=[1, 2, 3]]
[add_10(i) for i in [1, 2, 3]]
typeof(5)
typeof(Int64)
typeof(DataType)
type Tiger
taillength::Float64
coatcolor
end
tigger = Tiger(3.5,"orange")
sherekhan = typeof(tigger)(5.6,"fire")
abstract Cat
subtypes(Number)
subtypes(Cat)
typeof(5)
super(Int64)
super(Signed)
super(Real)
super(Number)
super(super(Signed))
super(Any)
type Lion <: Cat
mane_color
roar::String
end
Lion(roar::String) = Lion("green",roar)
type Panther <: Cat
eye_color
Panther() = new("green")
end
function meow(animal::Lion)
animal.roar
end
function meow(animal::Panther)
"grrr"
end
function meow(animal::Tiger)
"rawwwr"
end
meow(tigger)
meow(Lion("brown","ROAAR"))
meow(Panther())
issubtype(Tiger,Cat)
issubtype(Lion,Cat)
issubtype(Panther,Cat)
function pet_cat(cat::Cat)
println("The cat says $(meow(cat))")
end
pet_cat(Lion("42"))
try
pet_cat(tigger)
catch e
println(e)
end
function fight(t::Tiger,c::Cat)
println("The $(t.coatcolor) tiger wins!")
end
fight(tigger,Panther())
fight(tigger,Lion("ROAR"))
fight(t::Tiger,l::Lion) = println("The $(l.mane_color)-maned lion wins!")
fight(tigger,Panther())
fight(tigger,Lion("ROAR"))
fight(l::Lion,c::Cat) = println("The victorious cat says $(meow(c))")
fight(Lion("balooga!"),Panther())
try
fight(Panther(),Lion("RAWR"))
catch
end
fight(c::Cat,l::Lion) = println("The cat beats the Lion")
fight(Lion("RAR"),Lion("brown","rarrr"))
fight(l::Lion,l2::Lion) = println("The lions come to a tie")
fight(Lion("RAR"),Lion("brown","rarrr"))
square_area(l) = l * l
square_area(5)
code_native(square_area, (Int32,))
code_native(square_area, (Float32,))
code_native(square_area, (Float64,))
circle_area(r) = pi * r * r
circle_area(5)
code_native(circle_area, (Int32,))
code_native(circle_area, (Float64,))
برچسب:
نویسنده: باران صالحپور