Add Licensing and Contributing
[clnl] / src / main / clnl / nvm / controlflow.lisp
1 ; Copyright 2022 Frank Duncan (frank@consxy.com) under AGPL3.  See distributed LICENSE.txt.
2 (in-package #:clnl-nvm)
3
4 (defcommand ask (agent-or-agentset fn)
5  "ASK AGENT-OR-AGENTSET FN => RESULT
6
7   AGENT-OR-AGENTSET: AGENT | AGENTSET
8   RESULT: :undefined
9
10 ARGUMENTS AND VALUES:
11
12   FN: a function, run on each agent
13   AGENT: a NetLogo agent
14   AGENTSET: a NetLogo agentset
15
16 DESCRIPTION:
17
18   ASK is equivalent to ask in NetLogo.
19
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.
23
24   See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#ask"
25  (cond
26   ((agentset-p agent-or-agentset)
27    (let
28     ((iter (shufflerator (agentset-list agent-or-agentset))))
29     (loop
30      :for agent := (funcall iter)
31      :while agent
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))))
36   (t
37    (error "Ask requires an agentset or agent but got: ~A" agent-or-agentset))))
38
39 (defcommand stop ()
40  "STOP => RESULT
41
42   RESULT: :undefined
43
44 DESCRIPTION:
45
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.
49
50   See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#stop"
51  (error (make-condition 'stop)))