Skip to content

Commit 6f386fa

Browse files
Vexatosleios
authored andcommitted
Added exclamation marks to mutating julia functions (#526)
Also improved typing of bogo sort.
1 parent 8871209 commit 6f386fa

File tree

6 files changed

+19
-19
lines changed

6 files changed

+19
-19
lines changed

contents/bogo_sort/code/julia/bogo.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@ function is_sorted(a::Vector{Float64})
99
return true
1010
end
1111

12-
function bogo_sort(a::Vector{Float64})
12+
function bogo_sort!(a::Vector{Float64})
1313
while(!is_sorted(a))
1414
shuffle!(a)
1515
end
1616
end
1717

1818
function main()
19-
a = [1, 3, 2, 4]
20-
bogo_sort(a)
19+
a = [1.0, 3.0, 2.0, 4.0]
20+
bogo_sort!(a)
2121
println(a)
2222
end
2323

contents/bubble_sort/code/julia/bubble.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
function bubble_sort(a::Vector{Float64})
1+
function bubble_sort!(a::Vector{Float64})
22
n = length(a)
33
for i = 1:n
44
for j = 1:n-1
@@ -12,7 +12,7 @@ end
1212

1313
function main()
1414
a = [1., 3, 2, 4, 5, 10, 50, 7, 1.5, 0.3]
15-
bubble_sort(a)
15+
bubble_sort!(a)
1616
println(a)
1717
end
1818

contents/gaussian_elimination/code/julia/gaussian_elimination.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
function gaussian_elimination(A::Array{Float64,2})
1+
function gaussian_elimination!(A::Array{Float64,2})
22

33
rows = size(A,1)
44
cols = size(A,2)
@@ -64,7 +64,7 @@ function back_substitution(A::Array{Float64,2})
6464
end
6565

6666

67-
function gauss_jordan_elimination(A::Array{Float64,2})
67+
function gauss_jordan_elimination!(A::Array{Float64,2})
6868

6969
rows = size(A,1)
7070
cols = size(A,2)
@@ -97,10 +97,10 @@ function main()
9797
1 2 3 4;
9898
3 -4 0 10]
9999

100-
gaussian_elimination(A)
100+
gaussian_elimination!(A)
101101
println(A)
102102

103-
gauss_jordan_elimination(A)
103+
gauss_jordan_elimination!(A)
104104
println(A)
105105

106106
soln = back_substitution(A)

contents/graham_scan/code/julia/graham.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ function ccw(a::Point, b::Point, c::Point)
77
return ((b.x - a.x)*(c.y - a.y) - (b.y - a.y)*(c.x - a.x))
88
end
99

10-
function graham_scan(points::Vector{Point})
10+
function graham_scan!(points::Vector{Point})
1111
N = length(points)
1212

1313
# Place the lowest point at the start of the array
@@ -51,7 +51,7 @@ function main()
5151
Point(-1,-1), Point(-10,11), Point(-6,15), Point(-6,-8), Point(15,-9),
5252
Point(7,-7), Point(-2,-9), Point(6,-5), Point(0,14), Point(2,8)
5353
]
54-
hull = graham_scan(points)
54+
hull = graham_scan!(points)
5555
println(hull)
5656
end
5757

contents/huffman_encoding/code/julia/huffman.jl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,15 @@ const Node = Union{Leaf, Branch}
1616
isbranch(branch::Branch) = true
1717
isbranch(other::T) where {T} = false
1818

19-
function codebook_recurse(leaf::Leaf, code::String,
19+
function codebook_recurse!(leaf::Leaf, code::String,
2020
dict::Dict{Char,String})
2121
dict[leaf.key] = code
2222
end
2323

24-
function codebook_recurse(branch::Branch, code::String,
24+
function codebook_recurse!(branch::Branch, code::String,
2525
dict::Dict{Char,String})
26-
codebook_recurse(branch.left, string(code, "1"), dict)
27-
codebook_recurse(branch.right, string(code, "0"), dict)
26+
codebook_recurse!(branch.left, string(code, "1"), dict)
27+
codebook_recurse!(branch.right, string(code, "0"), dict)
2828
end
2929

3030
# This will depth-first search through the tree
@@ -33,7 +33,7 @@ end
3333
# This outputs encoding Dict to be used for encoding
3434
function create_codebook(n::Node)
3535
codebook = Dict{Char,String}()
36-
codebook_recurse(n, "", codebook)
36+
codebook_recurse!(n, "", codebook)
3737
return codebook
3838
end
3939

contents/split-operator_method/code/julia/split_op.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ function init(par::Param, voffset::Float64, wfcoffset::Float64)
6262
end
6363

6464
# Function for the split-operator loop
65-
function split_op(par::Param, opr::Operators)
65+
function split_op!(par::Param, opr::Operators)
6666

6767
for i = 1:par.timesteps
6868
# Half-step in real space
@@ -126,7 +126,7 @@ function calculate_energy(par, opr)
126126
energy_final = 0
127127
for i = 1:length(energy_k)
128128
energy_final += real(energy_k[i] + energy_r[i])
129-
end
129+
end
130130

131131
return energy_final*par.dx
132132
end
@@ -137,7 +137,7 @@ function main()
137137

138138
# Starting wavefunction slightly offset so we can see it change
139139
opr = init(par, 0.0, -1.00)
140-
split_op(par, opr)
140+
split_op!(par, opr)
141141

142142
energy = calculate_energy(par, opr)
143143
println("Energy is: ", energy)

0 commit comments

Comments
 (0)