JuliaTeachingCTU / Scientific-Programming-in-Julia

Repository for B0M36SPJ
https://juliateachingctu.github.io/Scientific-Programming-in-Julia/dev/
MIT License
76 stars 12 forks source link

Improving Lab 02 & 03 #61

Closed nmheim closed 1 year ago

nmheim commented 2 years ago

Lab 02

Improvement suggestions from last year

Lab 03

Improvement suggestions from last year

EcosystemCore.mates(::Animal{S,Female}, ::Animal{S,Male}) where S<:Species = true
EcosystemCore.mates(::Animal{S,Male}, ::Animal{S,Female}) where S<:Species = true
EcosystemCore.mates(::Agent, ::Agent) = false

##########  Additional functionality  ##########################################

function simulate!(world::World, iters::Int; cb=()->())
    for i in 1:iters
        world_step!(world)
        cb()
    end
end

agent_count(p::Plant) = size(p)/EcosystemCore.max_size(p)
agent_count(::Animal) = 1
agent_count(as::Vector{<:Agent}) = sum(agent_count,as)

function agent_count(w::World)
    function op(d::Dict,a::Agent{S}) where S<:Species
        n = nameof(S)
        if n in keys(d)
            d[n] += agent_count(a)
        else
            d[n] = agent_count(a)
        end
        return d
    end
    foldl(op, w.agents |> values |> collect, init=Dict{Symbol,Real}())
end

function every_nth(f::Function, n::Int)
    i = 1
    function callback(args...)
        # display(i) # comment this out to see out the counter increases
        if i == n
            f(args...)
            i = 1
        else
            i += 1
        end
    end
end