Code reformat - Break up nvm files, package declaration based on dictionary grouping
[clnl] / src / main / nvm / controlflow.lisp
1 (in-package #:clnl-nvm)
2
3 (defun ask (agent-or-agentset fn)
4  "ASK AGENT-OR-AGENTSET FN => RESULT
5
6   AGENT-OR-AGENTSET: AGENT | AGENTSET
7
8 ARGUMENTS AND VALUES:
9
10   FN: a function, run on each agent
11   RESULT: undefined, commands don't return
12   AGENT: a NetLogo agent
13   AGENTSET: a NetLogo agentset
14
15 DESCRIPTION:
16
17   ASK is equivalent to ask in NetLogo.
18
19   The specified AGENTSET or AGENT runs the given FN.  In the case of an
20   AGENTSET, the order in which the agents are run is random each time,
21   and only agents that are in the set at the beginning of the call.
22
23   See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#ask"
24  (cond
25   ((agentset-p agent-or-agentset)
26    (let
27     ((iter (shufflerator (agentset-list agent-or-agentset))))
28     (loop
29      :for agent := (funcall iter)
30      :while agent
31      :do (when (not (and (turtle-p agent) (= -1 (turtle-who agent))))
32           (let ((*myself* *self*) (*self* agent)) (with-stop-and-death-handler (funcall fn)))))))
33   ((agent-p agent-or-agentset)
34    (let ((*myself* *self*) (*self* agent-or-agentset)) (with-stop-and-death-handler (funcall fn))))
35   (t
36    (error "Ask requires an agentset or agent but got: ~A" agent-or-agentset))))
37
38 (defun stop ()
39  "STOP => RESULT
40
41 ARGUMENTS AND VALUES:
42
43   RESULT: undefined
44
45 DESCRIPTION:
46
47   Returns from the current stop block, which will halt the currently running
48   thing, be that the program, current ask block, or procedure.  Stop has odd
49   semantics that are best gleaned from the actual NetLogo manual.
50
51   See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#stop"
52  (error (make-condition 'stop)))