X-Git-Url: https://code.consxy.com/gitweb/gitweb.cgi?a=blobdiff_plain;f=src%2Fmain%2Fnvm%2Fnvm.lisp;h=a59247442d02fa2fa5a927865ae2a0dacd8023ac;hb=1d54233;hp=f8fdd84bde3a72ecdc4c8e87f6db905e5c42ca44;hpb=0a00e056cd1a8f022128525ac3cf3494cc36159d;p=clnl diff --git a/src/main/nvm/nvm.lisp b/src/main/nvm/nvm.lisp index f8fdd84..a592474 100644 --- a/src/main/nvm/nvm.lisp +++ b/src/main/nvm/nvm.lisp @@ -100,7 +100,7 @@ DESCRIPTION: each time it's used, so changes depending on the state of the engine. See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#patches" - *patches*) + :patches) (defun turtles () "TURTLES => ALL-TURTLES @@ -117,58 +117,96 @@ DESCRIPTION: each time it's used, so changes depending on the state of the engine. See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#turtles" - *turtles*) + :turtles) -(defun ask (agent-set fn) - "ASK AGENT-SET FN => RESULT +(defun ask (agent-or-agent-set fn) + "ASK AGENT-OR-AGENT-SET FN => RESULT + + AGENT-OR-AGENT-SET: AGENT | AGENT-SET ARGUMENTS AND VALUES: - AGENT-SET: a NetLogo agentset FN: a function, run on each agent RESULT: undefined, commands don't return + AGENT: a NetLogo agent + AGENT-SET: a NetLogo agentset DESCRIPTION: ASK is equivalent to ask in NetLogo. - The specified AGENT-SET runs the given FN. The order in which the agents - are run is random each time, and only agents that are in the set at the - beginning of the call. + The specified AGENT-SET or AGENT runs the given FN. In the case of an + AGENT-SET, the order in which the agents are run is random each time, + and only agents that are in the set at the beginning of the call. See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#ask" - (let - ((iter (shufflerator (agent-set-list agent-set)))) - (loop - :for agent := (funcall iter) - :while agent - :do (let ((*myself* *self*) (*self* agent)) (funcall fn))))) + (cond + ((agent-set-p agent-or-agent-set) + (let + ((iter (shufflerator (agent-set-list agent-or-agent-set)))) + (loop + :for agent := (funcall iter) + :while agent + :do (let ((*myself* *self*) (*self* agent)) (funcall fn))))) + ((agent-p agent-or-agent-set) + (let ((*myself* *self*) (*self* agent-or-agent-set)) (funcall fn))) + (t + (error "Ask requires an agent-set or agent but got: ~A" agent-or-agent-set)))) + +(defun count (agent-set) + "COUNT AGENT-SET => N -(defun of (fn agent-set) - "OF FN AGENT-SET => RESULT +ARGUMENTS AND VALUES: + + AGENT-SET: a NetLogo agentset + N: a number + +DESCRIPTION: + + COUNT is equivalent to count in NetLogo. Returns N, the number of + agents in AGENT-SET. + + See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#count" + (coerce (length (agent-set-list agent-set)) 'double-float)) + +(defun of (fn agent-or-agent-set) + "OF FN AGENT-OR-AGENT-SET => RESULT + + AGENT-OR-AGENT-SET: AGENT | AGENT-SET + RESULT: RESULT-LIST | RESULT-VALUE ARGUMENTS AND VALUES: FN: a function, run on each agent + AGENT: a NetLogo agent AGENT-SET: a NetLogo agentset - RESULT: a list + RESULT-LIST: a list + RESULT-VALUE: a single value DESCRIPTION: OF is equivalent to of in NetLogo. - The specified AGENT-SET runs the given FN. The order in which the agents - are run is random each time, and only agents that are in the set at the - beginning of the call. A list is returned of the returned valuse of - FN. + The specified AGENT-SET or AGENT runs the given FN. In the case of an + AGENT-SET, the order in which the agents are run is random each time, + and only agents that are in the set at the beginning of the call. + + RESULT-LIST is returned when the input is an AGENT-SET, but RESULT-VALUE + is returned when only passed an AGENT. See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#of" - (let - ((iter (shufflerator (agent-set-list agent-set)))) - (loop - :for agent := (funcall iter) - :while agent - :collect (let ((*myself* *self*) (*self* agent)) (funcall fn))))) + (cond + ((agent-set-p agent-or-agent-set) + (let + ((iter (shufflerator (agent-set-list agent-or-agent-set)))) + (loop + :for agent := (funcall iter) + :while agent + :collect (let ((*myself* *self*) (*self* agent)) (funcall fn))))) + ((agent-p agent-or-agent-set) + (let ((*myself* *self*) (*self* agent-or-agent-set)) (funcall fn))) + (t + (error "Of requires an agent-set or agent but got: ~A" agent-or-agent-set)))) (defun shufflerator (agent-set-list) (let