1 ; Copyright 2022 Frank Duncan (frank@consxy.com) under AGPL3. See distributed LICENSE.txt.
2 (in-package #:clnl-nvm)
4 (defcommand ask (agent-or-agentset fn)
5 "ASK AGENT-OR-AGENTSET FN => RESULT
7 AGENT-OR-AGENTSET: AGENT | AGENTSET
12 FN: a function, run on each agent
13 AGENT: a NetLogo agent
14 AGENTSET: a NetLogo agentset
18 ASK is equivalent to ask in NetLogo.
20 The specified AGENTSET or AGENT runs the given FN. In the case of an
21 AGENTSET, the order in which the agents are run is random each time,
22 and only agents that are in the set at the beginning of the call.
24 See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#ask"
26 ((agentset-p agent-or-agentset)
28 ((iter (shufflerator (agentset-list agent-or-agentset))))
30 :for agent := (funcall iter)
32 :do (when (not (and (turtle-p agent) (= -1 (turtle-who agent))))
33 (let ((*myself* *self*) (*self* agent)) (with-stop-and-death-handler (funcall fn)))))))
34 ((agent-p agent-or-agentset)
35 (let ((*myself* *self*) (*self* agent-or-agentset)) (with-stop-and-death-handler (funcall fn))))
37 (error "Ask requires an agentset or agent but got: ~A" agent-or-agentset))))
46 Returns from the current stop block, which will halt the currently running
47 thing, be that the program, current ask block, or procedure. Stop has odd
48 semantics that are best gleaned from the actual NetLogo manual.
50 See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#stop"
51 (error (make-condition 'stop)))