1 ; Copyright 2022 Frank Duncan (frank@consxy.com) under AGPL3. See distributed LICENSE.txt.
2 (in-package #:clnl-nvm)
4 (defun count (agentset)
9 AGENTSET: a NetLogo agentset
14 COUNT is equivalent to count in NetLogo. Returns N, the number of
17 See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#count"
18 (coerce (length (agentset-list agentset)) 'double-float))
20 (defun of (fn agent-or-agentset)
21 "OF FN AGENT-OR-AGENTSET => RESULT
23 AGENT-OR-AGENTSET: AGENT | AGENTSET
24 RESULT: RESULT-LIST | RESULT-VALUE
28 FN: a function, run on each agent
29 AGENT: a NetLogo agent
30 AGENTSET: a NetLogo agentset
32 RESULT-VALUE: a single value
36 OF is equivalent to of in NetLogo.
38 The specified AGENTSET or AGENT runs the given FN. In the case of an
39 AGENTSET, the order in which the agents are run is random each time,
40 and only agents that are in the set at the beginning of the call.
42 RESULT-LIST is returned when the input is an AGENTSET, but RESULT-VALUE
43 is returned when only passed an AGENT.
45 See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#of"
47 ((agentset-p agent-or-agentset)
49 ((iter (shufflerator (agentset-list agent-or-agentset))))
51 :for agent := (funcall iter)
53 :collect (let ((*myself* *self*) (*self* agent)) (funcall fn)))))
54 ((agent-p agent-or-agentset)
55 (let ((*myself* *self*) (*self* agent-or-agentset)) (funcall fn)))
57 (error "Of requires an agentset or agent but got: ~A" agent-or-agentset))))
59 (defun one-of (list-or-agentset)
60 "ONE-OF LIST-OR-AGENTSET => RESULT
62 LIST-OR-AGENTSET: LIST | AGENTSET
63 RESULT: RANDOM-VALUE | RANDOM-AGENT | :nobody
68 AGENTSET: An agent set
69 RANDOM-VALUE: a value in LIST
70 RANDOM-AGENT: an agent if AGENTSET is non empty
74 From an AGENTSET, returns a RANDOM-AGENT. If the agentset is empty, returns :nobody.
75 From a list, returns a RANDOM-VALUE. If the list is empty, an error occurs.
77 See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#one-of"
79 ((agentset-p list-or-agentset)
81 ((agentset-list (agentset-list list-or-agentset))
82 (length (length agentset-list)))
83 (if (zerop length) :nobody (nth (clnl-random:next-int length) agentset-list))))
84 ((listp list-or-agentset)
86 ((length (length list-or-agentset)))
88 (error "one-of requires a nonempty list")
89 (nth (clnl-random:next-int length) list-or-agentset))))
90 (t (error "one-of requires a list or agentset"))))
93 "PATCHES => ALL-PATCHES
97 ALL-PATCHES: a NetLogo agentset, all patches
101 Reports the agentset consisting of all the patches.
103 This agentset is special in that it represents the living patches
104 each time it's used, so changes depending on the state of the engine.
106 See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#patches"
110 "TURTLES => ALL-TURTLES
112 ARGUMENTS AND VALUES:
114 ALL-TURTLES: a NetLogo agentset, all turtles
118 Reports the agentset consisting of all the turtles.
120 This agentset is special in that it represents the living turtles
121 each time it's used, so changes depending on the state of the engine.
123 See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#turtles"
126 (defun with (agentset fn)
127 "WITH AGENTSET FN => RESULT-AGENTSET
129 ARGUMENTS AND VALUES:
131 AGENTSET: a NetLogo agentset
132 FN: a boolean function, run on each agent to determine if included
133 RESULT-AGENTSET: an agentset of valid agents
137 WITH is equivalent to with in NetLogo.
139 Returns a new agentset containing only those agents that reported true
142 See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#with"
146 (let ((*myself* *self*) (*self* agent)) (funcall fn)))
147 (agentset-list agentset))
148 (agentset-breed agentset)))