1 (in-package #:clnl-nvm)
3 (defun count (agentset)
8 AGENTSET: a NetLogo agentset
13 COUNT is equivalent to count in NetLogo. Returns N, the number of
16 See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#count"
17 (coerce (length (agentset-list agentset)) 'double-float))
19 (defun of (fn agent-or-agentset)
20 "OF FN AGENT-OR-AGENTSET => RESULT
22 AGENT-OR-AGENTSET: AGENT | AGENTSET
23 RESULT: RESULT-LIST | RESULT-VALUE
27 FN: a function, run on each agent
28 AGENT: a NetLogo agent
29 AGENTSET: a NetLogo agentset
31 RESULT-VALUE: a single value
35 OF is equivalent to of in NetLogo.
37 The specified AGENTSET or AGENT runs the given FN. In the case of an
38 AGENTSET, the order in which the agents are run is random each time,
39 and only agents that are in the set at the beginning of the call.
41 RESULT-LIST is returned when the input is an AGENTSET, but RESULT-VALUE
42 is returned when only passed an AGENT.
44 See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#of"
46 ((agentset-p agent-or-agentset)
48 ((iter (shufflerator (agentset-list agent-or-agentset))))
50 :for agent := (funcall iter)
52 :collect (let ((*myself* *self*) (*self* agent)) (funcall fn)))))
53 ((agent-p agent-or-agentset)
54 (let ((*myself* *self*) (*self* agent-or-agentset)) (funcall fn)))
56 (error "Of requires an agentset or agent but got: ~A" agent-or-agentset))))
58 (defun one-of (list-or-agentset)
59 "ONE-OF LIST-OR-AGENTSET => RESULT
61 LIST-OR-AGENTSET: LIST | AGENTSET
62 RESULT: RANDOM-VALUE | RANDOM-AGENT | :nobody
67 AGENTSET: An agent set
68 RANDOM-VALUE: a value in LIST
69 RANDOM-AGENT: an agent if AGENTSET is non empty
73 From an AGENTSET, returns a RANDOM-AGENT. If the agentset is empty, returns :nobody.
74 From a list, returns a RANDOM-VALUE. If the list is empty, an error occurs.
76 See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#one-of"
78 ((agentset-p list-or-agentset)
80 ((agentset-list (agentset-list list-or-agentset))
81 (length (length agentset-list)))
82 (if (zerop length) :nobody (nth (clnl-random:next-int length) agentset-list))))
83 ((listp list-or-agentset)
85 ((length (length list-or-agentset)))
87 (error "one-of requires a nonempty list")
88 (nth (clnl-random:next-int length) list-or-agentset))))
89 (t (error "one-of requires a list or agentset"))))
92 "PATCHES => ALL-PATCHES
96 ALL-PATCHES: a NetLogo agentset, all patches
100 Reports the agentset consisting of all the patches.
102 This agentset is special in that it represents the living patches
103 each time it's used, so changes depending on the state of the engine.
105 See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#patches"
109 "TURTLES => ALL-TURTLES
111 ARGUMENTS AND VALUES:
113 ALL-TURTLES: a NetLogo agentset, all turtles
117 Reports the agentset consisting of all the turtles.
119 This agentset is special in that it represents the living turtles
120 each time it's used, so changes depending on the state of the engine.
122 See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#turtles"
125 (defun with (agentset fn)
126 "WITH AGENTSET FN => RESULT-AGENTSET
128 ARGUMENTS AND VALUES:
130 AGENTSET: a NetLogo agentset
131 FN: a boolean function, run on each agent to determine if included
132 RESULT-AGENTSET: an agentset of valid agents
136 WITH is equivalent to with in NetLogo.
138 Returns a new agentset containing only those agents that reported true
141 See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#with"
145 (let ((*myself* *self*) (*self* agent)) (funcall fn)))
146 (agentset-list agentset))
147 (agentset-breed agentset)))