Quantcast
Channel: First steps - JuliaLang
Viewing all articles
Browse latest Browse all 2795

A question about closures

$
0
0

Hi,

I’m trying to work out how to construct a closure for the following case:

The Agents package has a random_agent(agent, condition) function and I want to call it with a condition closure. I though the following would work:

using Agents

mutable struct MyAgent <: AbstractAgent
    id::Int64
    state::Int64
end

has_state(agent, state) = agent.state == state

model = ABM(MyAgent)
add_agent!(MyAgent(1, 1), model)
add_agent!(MyAgent(2, 1), model)
condition = state -> has_state(agent, state)

random_agent(model, condition(1))

But then I get the following error:

ERROR: UndefVarError: agent not defined
Stacktrace:
 [1] (::var"#7#8")(::Int64) at ./none:1
 [2] top-level scope at none:1

What am I doing wrong?

Thanks in advance,
Stef

7 posts - 3 participants

Read full topic


Viewing all articles
Browse latest Browse all 2795

Trending Articles