Skip to content

Commit 90dbed1

Browse files
committed
hw2 students question
1 parent fcb2aeb commit 90dbed1

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
## agent_count
2+
### recommended option
3+
agent_count(a::Animal) = 1.0
4+
agent_count(p::Plant) = p.size / p.max_size
5+
6+
## agent_count(agents::Vector{<:Agent})
7+
### recommended option
8+
agent_count(as::Vector{<:Agent}) = sum(agent_count,as)
9+
10+
## agent_count(w::World)
11+
### recommended option (little fancy)
12+
function agent_count(w::World)
13+
function op(d::Dict,a::A) where A<:Agent
14+
n = nameof(A)
15+
d[n] = haskey(d,n) ? d[n]+agent_count(a) : agent_count(a)
16+
return d
17+
end
18+
reduce(op, w.agents |> values, init=Dict{Symbol,Float64}())
19+
end

0 commit comments

Comments
 (0)