Move from github, collapse gltk and strictmath, add candle
[clnl] / src / main / clnl / nvm / controlflow.lisp
1 (in-package #:clnl-nvm)
2
3 (defcommand ask (agent-or-agentset fn)
4  "ASK AGENT-OR-AGENTSET FN => RESULT
5
6   AGENT-OR-AGENTSET: AGENT | AGENTSET
7   RESULT: :undefined
8
9 ARGUMENTS AND VALUES:
10
11   FN: a function, run on each agent
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 (defcommand stop ()
39  "STOP => RESULT
40
41   RESULT: :undefined
42
43 DESCRIPTION:
44
45   Returns from the current stop block, which will halt the currently running
46   thing, be that the program, current ask block, or procedure.  Stop has odd
47   semantics that are best gleaned from the actual NetLogo manual.
48
49   See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#stop"
50  (error (make-condition 'stop)))