From 1d5423397baed94b051f404da7774724e8625e47 Mon Sep 17 00:00:00 2001 From: Frank Duncan Date: Sat, 30 Apr 2016 12:14:28 -0500 Subject: [PATCH] Prims - Update ask, of to take agents --- src/main/nvm/base.lisp | 9 +++++ src/main/nvm/nvm.lisp | 72 +++++++++++++++++++++++++-------------- src/test/simpletests.lisp | 6 ++++ 3 files changed, 62 insertions(+), 25 deletions(-) diff --git a/src/main/nvm/base.lisp b/src/main/nvm/base.lisp index 1df5022..8f1595c 100644 --- a/src/main/nvm/base.lisp +++ b/src/main/nvm/base.lisp @@ -19,3 +19,12 @@ ((eql agent-set :patches) *patches*) ((and (listp agent-set) (eql :agent-set (car agent-set))) (cdr agent-set)) (t (error "Doesn't seem to be an agent-set: ~A" agent-set)))) + +(defun agent-set-p (o) + (or + (eql o :turtles) + (eql o :patches) + (and (listp o) (eql :agent-set (car o))))) + +(defun agent-p (o) + (or (turtle-p o) (patch-p o))) diff --git a/src/main/nvm/nvm.lisp b/src/main/nvm/nvm.lisp index da42870..a592474 100644 --- a/src/main/nvm/nvm.lisp +++ b/src/main/nvm/nvm.lisp @@ -119,30 +119,39 @@ DESCRIPTION: See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#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 @@ -160,31 +169,44 @@ DESCRIPTION: See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#count" (coerce (length (agent-set-list agent-set)) 'double-float)) -(defun of (fn agent-set) - "OF FN AGENT-SET => RESULT +(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 diff --git a/src/test/simpletests.lisp b/src/test/simpletests.lisp index 22b2d19..b68cc1a 100644 --- a/src/test/simpletests.lisp +++ b/src/test/simpletests.lisp @@ -166,6 +166,12 @@ (defreportertestwithsetup "one-of 3" "crt 10" "one-of turtles" "(turtle 5)" "A056ED8BF26A69FB4437E79F263E362C27F8820E") +(defreportertestwithsetup "one-of / of" "crt 10" "[ color ] of one-of turtles" "65" + "A056ED8BF26A69FB4437E79F263E362C27F8820E") + +(defsimplecommandtest "one-of / ask" "crt 10 ask one-of turtles [ fd 1 ]" + "40106C3853F3870AAE37F232353115968A3A02F6") + (defsimplecommandtest "color 1" "crt 10 ask turtles [ set color green ]" "20943094E2C70D5A12AC6EEB29E8E9E2D21BD87D") -- 2.25.1