Add type signatures to functions

This commit is contained in:
Dimitri Lozeve 2020-02-20 21:47:45 +01:00
parent 0837a30169
commit bd97cffcc1

View file

@ -6,13 +6,13 @@ using Tables
export loadplants, loadgarden, loadcosts, update!, randomgardenevolution!, outputgarden export loadplants, loadgarden, loadcosts, update!, randomgardenevolution!, outputgarden
function loadplants() function loadplants()::Vector{String}
plants = readlines("data/plants.txt") plants = readlines("data/plants.txt")
@info "loaded $(length(plants)) plants" @info "loaded $(length(plants)) plants"
plants plants
end end
function loadgarden(plants) function loadgarden(plants::Vector{String})::Tuple{Matrix{Int}, Matrix{Bool}}
garden = CSV.read("data/garden.csv") garden = CSV.read("data/garden.csv")
garden = coalesce.(garden, "") garden = coalesce.(garden, "")
mask = convert(Matrix, garden .== "empty") mask = convert(Matrix, garden .== "empty")
@ -23,7 +23,7 @@ function loadgarden(plants)
garden, mask garden, mask
end end
function loadcosts() function loadcosts()::Matrix{Float64}
df = CSV.read("data/costs.csv") df = CSV.read("data/costs.csv")
df = coalesce.(df, 0) # replace missing values by 0 df = coalesce.(df, 0) # replace missing values by 0
costs = convert(Matrix, df[:, 2:end]) costs = convert(Matrix, df[:, 2:end])
@ -32,7 +32,7 @@ function loadcosts()
costs = Float64.(max.(costs, permutedims(costs))) costs = Float64.(max.(costs, permutedims(costs)))
end end
function randomindex(mask::Matrix{Bool}) function randomindex(mask::Matrix{Bool})::Int
while true while true
i = rand(1:length(mask)) i = rand(1:length(mask))
if mask[i] if mask[i]
@ -48,7 +48,7 @@ function swap!(garden::Matrix{Int}, i::Int, j::Int)
garden garden
end end
function neighbours(garden::Matrix{Int}, idx) function neighbours(garden::Matrix{Int}, idx::Int)::Vector{Int}
m, n = size(garden) m, n = size(garden)
j, i = divrem(idx - 1, m) j, i = divrem(idx - 1, m)
i += 1 i += 1
@ -61,7 +61,7 @@ function neighbours(garden::Matrix{Int}, idx)
] ]
end end
function deltacost(garden::Matrix{Int}, costs::Matrix{Float64}, i::Int, j::Int) function deltacost(garden::Matrix{Int}, costs::Matrix{Float64}, i::Int, j::Int)::Float64
cost = 0 cost = 0
for k in neighbours(garden, i) for k in neighbours(garden, i)
cost += costs[k, garden[j]] - costs[k, garden[i]] cost += costs[k, garden[j]] - costs[k, garden[i]]